gpt4 book ai didi

c++ - ld 通过参数返回 1 个退出状态错误

转载 作者:行者123 更新时间:2023-11-28 05:00:42 24 4
gpt4 key购买 nike

这编译得很好。

const int NUM_PLAYERS = 10;

struct wrType
{
string name[NUM_PLAYERS];
string team[NUM_PLAYERS];
int catches[NUM_PLAYERS];
int yards[NUM_PLAYERS];
int td[NUM_PLAYERS];
double ypc[NUM_PLAYERS];
};

void populateList(wrType[], ifstream&);

int main(int args, char* argv[])
{
wrType *wide;
char select;
ifstream in;
in.open(argv[1]);

/*populateList(wrType wide, ifstream in); code causing error*/

}

void populateList(wrType wide, ifstream in)
{
};

一旦我取消注释以下代码

'populateList(wide, in);'

出现以下错误

'ld returned 1 exit status error'

编译后。看了很多论坛都没能解决问题。我们最近接触了结构和类,但我不确定我是否误解了一个概念或如何让它发挥作用。

该代码应该通过命令行传递一个文件,然后能够访问它并根据足球运动员的不同统计数据(即 10 名球员)使用冒泡排序进行排序。我不想让我的手被牵着,这就是为什么我把剩下的留给以后再挣扎的原因。该代码在没有该函数的情况下在 main 中工作,但是一旦我尝试使用我拥有的代码调用该函数,它就会给我一个错误。

此外,当我尝试将它们作为指针传递时,它给我一个无法将 myType* 转换为 myType

更新完整错误:

'In function main':
(.text+0xad): undefined reference to
populateList(wrType*, std::basic_ifstream >&)' collect2: error: ld returned 1 exit status'

编辑 2:带有

的代码

'populateList(wrType wide, ifstream in);'

返回以下错误:

在函数‘int main(int, char**)’中:

34:22: error: expected primary-expression before ‘wide’ populateList(wrType wide, ifstream in); ^ 34:37: error: expected primary-expression before ‘in’ populateList(wrType wide, ifstream in);

最佳答案

这段代码有很多问题。

  1. struct wrType
    {
    string name[NUM_PLAYERS];
    ...
    };

    您需要一个记录数组,而不是并行数组的记录。删除上述 struct 中的所有 [...]

  2. wrType *wide;

    您需要这些结构的数组,而不是指针。

    wrType wide[NUM_PLAYERS];
  3. populateList(wrType wide, ifstream in);

    这不是正确的函数调用语法。

  4. void populateList(wrType wide, ifstream in)

    这与之前的声明不匹配

    void populateList(wrType[], ifstream&);

您需要围绕此声明构建整个程序。首先,使您的 populateList 定义与该行一致

void populateList(wrType[] wide, ifstream& in)
{
}

然后填写正文。请注意,像

这样的行
in >> wide.name[x]; 

不再正确,您需要更改它们。

关于c++ - ld 通过参数返回 1 个退出状态错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46126791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com