sách gpt4 ai đã đi

c++ - 使用 TextPad/G++ 链接和编译 C++ 文件时出错,可能(可能)只是语法?

In lại 作者:太空宇宙 更新时间:2023-11-04 16:15:39 32 4
mua khóa gpt4 Nike

这很可能是我的语法错误,因为我对在 C++ 中使用多个文件和结构(特别是将结构传递给函数)还很陌生。这是三个文件:

主要.cpp:

#include 
#include
#include
#include "common.h"
sử dụng không gian tên std;

void honorStatus(int, student studentList[]);

int main(void)
{
int header;
string filename;
ifstream inputFile;
student studentList[MAX_STUDENTS];

// Get filename from user and try to open file
cout << "Please enter a filename: ";
cin >> filename;

inputFile.open(filename.c_str());

// If file cannot be opened, output error message and close program
if (inputFile.fail())
{
cout << "Input file could not be opened. Please try again." << endl;
return 1;
}

// Get header number from file. If header is larger than max number
// of students, error is output and program is closed
inputFile >> header;
if (header > MAX_STUDENTS)
{
cout << "Number of students has exceeded maximum of " << MAX_STUDENTS
<< ". Please try again." << endl;
return 1;
}

// Read file information (student ID, hours, and GPA) into struct array
for (int i = 0; i < header; i++)
{
inputFile >> studentList[i].ID >> studentList[i].hours >> studentList[i].GPA;
}

// Close the file
inputFile.close();

// Calls function honorStatus
honorStatus(header, studentList);

trả về 0;
}

函数.cpp:

#include 
#include "common.h"
sử dụng không gian tên std;

// Function to determine classification and honors society eligibility requirements
// of each student, outputting this information and the number of students eligible
void honorStatus(int fheader, student fstudentList[])
{
int cnt = 0;

for (int i = 0; i < fheader; i++)
{
if (fstudentList[i].hours < 30)
{
cout << "Student #" << fstudentList[i].ID << " is a freshman with GPA of "
<< fstudentList[i].GPA << ". Not eligible." << endl;
}
else if (fstudentList[i].hours > 29 && fstudentList[i].hours < 60)
{
if (fstudentList[i].GPA >= 3.75)
{
cout << "Student #" << fstudentList[i].ID << " is a sophomore with GPA of "
<< fstudentList[i].GPA << ". Eligible." << endl;
cnt++;
}
khác
{
cout << "Student #" << fstudentList[i].ID << " is a sophomore with GPA of "
<< fstudentList[i].GPA << ". Not Eligible." << endl;
}
}
else if (fstudentList[i].hours > 59 && fstudentList[i].hours < 90)
{
if (fstudentList[i].GPA >= 3.5)
{
cout << "Student #" << fstudentList[i].ID << " is a junior with GPA of "
<< fstudentList[i].GPA << ". Eligible." << endl;
cnt++;
}
khác
{
cout << "Student #" << fstudentList[i].ID << " is a junior with GPA of "
<< fstudentList[i].GPA << ". Not eligible." << endl;
}
}
khác
{
if (fstudentList[i].GPA >= 3.25)
{
cout << "Student #" << fstudentList[i].ID << " is a senior with GPA of "
<< fstudentList[i].GPA << ". Eligible." << endl;
cnt++;
}
khác
{
cout << "Student #" << fstudentList[i].ID << " is a senior with GPA of "
<< fstudentList[i].GPA << ". Not eligible." << endl;
}
}
}

cout << "\nTotal number of students eligible for the Honor Society is " << cnt << "." << endl;
}

通用.h:

// Maximum number of students allowed
const int MAX_STUDENTS = 10;

// Struct for student info
struct student
{
int ID;
int hours;
float GPA;
};

当使用 TextPad/G++ 时,出现以下错误:

/cygdrive/c/Users/Korina/AppData/Local/Temp/ccxq9DAh.o:p7b.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccLa96oD.o:test.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/usr/lib/gcc/i686-pc-cygwin/4.8.2/../../../../i686-pc-cygwin/bin/ld: /cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o: bad reloc address 0x1b in section `.text$_ZNSt11char_traitsIcE7compareEPKcS2_j[__ZNSt11char_traitsIcE7compareEPKcS2_j]'
collect2: lỗi: ld trả về 1 trạng thái thoát

当使用在线 C++ 编译器 (CompileOnline) 时,我得到:

/tmp/ccIMwHEt.o: In function `main':
main.cpp:(.text+0x1cf): undefined reference to `honorStatus(int, student*)'
collect2: lỗi: ld trả về 1 trạng thái thoát

我无法找到关于如何设置 TextPad/G++ 以编译和链接多个文件的指南,但我的导师给出了一组简短的说明,我遵循了这些说明。这是它的设置方式:

TextPad/G++ Multiple File Compile

所以这可能是一个由两个部分组成的问题(我如何设置 TextPad 以正确编译/链接文件?为什么我的 honorStatus() 函数在 main.cpp 中未定义?)或者它可能只是是我的语法错误。老实说,我不确定。对不起,如果这有点长;我想包括尽可能多的细节。非常感谢任何帮助。

1 Câu trả lời

问题是您正在一起编译“*.cpp”。鉴于此

/cygdrive/c/Users/Korina/AppData/Local/Temp/ccxq9DAh.o:p7b.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccLa96oD.o:test.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/usr/lib/gcc/i686-pc-cygwin/4.8.2/../../../../i686-pc-cygwin/bin/ld: /cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o: bad reloc address 0x1b in section `.text$_ZNSt11char_traitsIcE7compareEPKcS2_j[__ZNSt11char_traitsIcE7compareEPKcS2_j]'
collect2: lỗi: ld trả về 1 trạng thái thoát

我们可以看到编译器一直在尝试将 p5.cpp、p7b.cpp 和 test.cpp 组合成一个可执行文件(也可能是其他 .cpp 文件)。

您实际上需要准确地告诉编译器您想将哪些文件一起构建到一个程序中。例如。

g++ main.cpp functs.cpp -o main.exe 

(我还建议将 -Wall -Wextra -Werror 添加到编译行,因为这样可以让编译器检测到小错误,这些小错误严格来说不是错误,但你可能发现了一些错误错误)

关于c++ - 使用 TextPad/G++ 链接和编译 C++ 文件时出错,可能(可能)只是语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23040698/

32 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com