【发布时间】:2018-09-07 11:39:31
【问题描述】:
我是一所大学的学生,正在为班级创建一个小型股票市场模拟器游戏。我们被要求专门使用结构而不是我更精通的类。
我正在尝试设置“Stock”结构对象数组,并尝试将 Stock 对象数组和 Stock 对象本身传递给一个函数(每个都在一个函数中)单独的文件。这是 Stock 结构。
struct Stock {
string FullName;
string Ticker;
string Info;
string Chairman;
double Revenue;
double AvgVol;
double High52;
double Low52;
double CurrentPrice;
Stock() {
FullName = "";
Ticker = "";
Info = "";
Chairman = "";
Revenue = 0;
AvgVol = 0;
High52 = 0;
Low52 = 0;
CurrentPrice = 0;
} };
这是它自己的文件,专门用于创建整个游戏中使用的各种结构。 这就是我尝试在主驱动函数中创建和调用股票的方式。
Stock Apple;
Stock Chipotle;
Stock Disney;
Stock Tesla;
Stock stockList[4] = { Apple, Chipotle, Disney, Tesla }; //Will access the stocks from this array from here on in
SetupStructs(stockList); //Function that creates the 4 company's information as struct objects to be utilized later
这是第三个文件中的SetupStructs函数,专门用于函数,以免阻塞主文件。
void SetupStructs(Stock stockList[]) {
stockList[0].FullName = "Apple Incorporated";
stockList[1].FullName = "Chipotle Mexican Grill Incorporated";
stockList[2].FullName = "Walt Disney Company";
stockList[3].FullName = "Tesla Motors Incorporated"; };
如果不是很明显,我已将其余 90% 的 SetupStructs 函数从我粘贴的内容中删除,以免不必要的代码堵塞页面。
问题是:“SetupStructs(stockList)”正在抛出错误:
argument of type "Stock *" is incompatible with parameter of type "Stock *"
我做错了什么?为什么会出现这个错误?为小论文道歉。
【问题讨论】:
-
你用的是什么编译器? Godbolt 似乎对一个精简的例子很满意:godbolt.org/g/Yu7P8N
-
@StephenNewell Visual Studio Community 2017。我下载了建议您在首次打开应用程序时使用的任何 C++ 游戏包。
-
Godbolt 在 Visual Studio 中看起来不错:godbolt.org/g/C2KKZB。您确定您提供的示例失败了吗?
-
@StephenNewell 嗯,这是 VS 目前向我抛出的 25 个错误之一,25 个错误中的 24 个都与我对 struct 对象的各种用法有关。
-
这是一个愚蠢的问题,结构必须在头文件中吗?我目前在 .cpp 文件中定义了我的结构对象。我从来没有过多考虑如何创建类,但我总是为每个头文件创建 1 个类,在这种情况下,我在 .cpp 文件中创建多个结构(据我所知,这几乎是类)。
标签: c++ arrays function c++11 struct