c++:没有重载函数的实例?

发布于 2024-11-04 00:14:48 字数 1441 浏览 0 评论 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

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

...

我的错误说的是“不存在重载函数 Stock::Stock(string stockExchange, string symbol, string company, doubleprice, int Shares) 的实例”。

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

// 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."

What am I doing wrong? I see it in my header file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2024-11-11 00:14:48

您尚未在 stock.h 头文件中包含 头文件,即使您在它。也许这就是导致此错误消息的原因(如果是这种情况,那么我会说这确实是一个坏消息)。

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

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

这是错误的。从中删除 Stock:: ,并使其像这样:

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

在类外部定义函数时需要 Stock::

You've not included <string> header file in stock.h header file, even though you're using std::string in it. 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:

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

which is wrong. Remove Stock:: from it, and make it like this:

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

Stock:: is required when defining the function outside the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文