簡體   English   中英

c++:沒有過載 function 的實例?

[英]c++: No instance of overloaded function?

// stock.h

#ifndef STOCK_H
#define STOCK_H

// declare Stock Class
class Stock
{
private:
    string StockExchange;
    string Symbol;
    string Company;
    double Price;
    int Shares;
public:
    Stock();
    Stock(string stockExchange, string symbol, string company, double price, int shares);
    void displayStockInfo();
    void setStockInfo(string stockExchange, string symbol, string company, double price, int shares);
    double getValue();
    bool operator < (Stock & aStock);
    bool Stock::operator > (Stock & aStock);
};

#endif

[休息]

//main.cpp

#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>

#include "stock.h"

using std::string;
using std::endl;
using std::cout;
using std::setw;
using std::ifstream;


// *******************************
// Stock class

Stock::Stock() {
    StockExchange = "";
    Symbol = "";
    Company = "";
    Price = 0.0;
    Shares = 0;
}

Stock::Stock(string stockExchange, string symbol, string company, double price, int shares) {
    StockExchange = stockExchange;
    Symbol = symbol;
    Company = company;
    Price = price;
    Shares = shares;
}


// end Stock class
// *******************************

...

我的錯誤表明“不存在過載 function Stock::Stock(string stockExchange, string symbol, string company, double price, int shares) 的實例。”

我究竟做錯了什么? 我在我的 header 文件中看到它。

您沒有在stock.h header 文件中包含<string> header 文件,即使您在其中使用std::string也是如此。 也許這是導致此錯誤消息的原因(如果是這種情況,那么我會說它確實是一條錯誤消息)。

另一個問題是,在Stock class 定義中,你寫了這個:

bool Stock::operator > (Stock & aStock);

這是錯誤的。 從中刪除Stock:: ,並使其如下所示:

bool operator > (const Stock & aStock);
               //^^^^ add this also (better)

Stock::在 class 之外定義 function 時需要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM