2011-04-29 104 views
0
// 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 

[破]C++:没有重载函数的实例?

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

... 

我的错误说沿着“没有重载函数股票的实例线::股票(字符串股票交易所,串符号,串公司,双重价格,INT股)存在的东西“。

我在做什么错?我在我的头文件中看到它。

+0

“我的错误说...”说哪里?通常会在通话时报告类似的错误。我在您提供的代码中看不到任何呼叫。 – AnT 2011-04-29 17:53:20

+1

将代码和实际错误消息发布到编译器报告错误的行。 – 2011-04-29 17:54:12

+0

将您的代码缩减为演示错误所需的最小示例,然后重新发布整个结果。 – NPE 2011-04-29 17:54:43

回答

2

您还没有在stock.h头文件中包含<string>头文件,即使您在其中使用std::string也是如此。也许这是导致这个错误信息(如果是这样的话,那么我会说它真的是一个坏消息)。

的另一个问题是,在Stock类的定义,你写这样的:

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

这是不对的。从中取出Stock::,并使其像这样:

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

Stock::定义类之外的功能时是必需的。

+0

这是因为未声明的类型自动为int吗? – schoetbi 2011-04-29 17:57:33

+0

@schoetbi不在C++中。 – 2011-04-29 18:00:56

+0

似乎是这样的问题。我很困惑把#include和类似的东西放在哪里。他们应该进入头文件吗?如果我的头文件#include 那么我的main.cpp会包含#include 会发生什么情况。我不会收到编译错误吗? – Skinner927 2011-04-29 18:01:03