简体   繁体   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

[break] [休息]

//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
// *******************************

...

My error says something along the lines of "no instance of overloaded function Stock::Stock(string stockExchange, string symbol, string company, double price, int shares) exists."我的错误表明“不存在过载 function Stock::Stock(string stockExchange, string symbol, string company, double price, int shares) 的实例。”

What am I doing wrong?我究竟做错了什么? I see it in my header file.我在我的 header 文件中看到它。

You've not included <string> header file in stock.h header file, even though you're using std::string in it.您没有在stock.h header 文件中包含<string> header 文件,即使您在其中使用std::string也是如此。 Maybe that is causing this error message (if that is the case, then I would say its really a bad message).也许这是导致此错误消息的原因(如果是这种情况,那么我会说它确实是一条错误消息)。

Another problem is that in Stock class definition, you've written this:另一个问题是,在Stock class 定义中,你写了这个:

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

which is wrong.这是错误的。 Remove Stock:: from it, and make it like this:从中删除Stock:: ,并使其如下所示:

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

Stock:: is required when defining the function outside the class. Stock::在 class 之外定义 function 时需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM