好的,因为我是一个相对较新的程序员,所以我正在通过youtube上的C++教程。我正在尝试学习如何在创建类时使用头文件等。在本教程中,我们学习如何使用箭头成员选择运算符。
https://www.youtube.com/watch?v=2RP4f9beidc&list=PLAE85DE8440AA6B83&index=42
我没有使用他正在使用的IDE的代码块。我正在用gedit编写代码,并使用Cygwin进行编译和运行。
任何帮助将不胜感激!
这是简单的代码:
Main.cpp
#include <iostream>
#include "Sally.h"
using namespace std;
int main(){
Sally sallyObject;
Sally *sallyPointer = &sallyObject;
sallyObject.printCrap();
sallyPointer->printCrap();
}
萨莉
#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally()
{
}
void Sally::printCrap(){
cout << "did someone say steak?" << endl;
}
萨莉·h
#ifndef SALLY_H
#define SALLY_H
#include <iostream>
class Sally
{
public:
Sally();
void printCrap();
protected:
private:
};
#endif // SALLY_H
错误代码:
/tmp/ccfOEJJF.o:Main.cpp:(.text+0x15):对
Sally::Sally()'/tmp/ccfOEJJF.o:Main.cpp:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol
Sally::Sally()的 undefined reference /tmp/ccfOEJJF.o:Main.cpp:(.text+0x29):对
Sally::printCrap()'/tmp/ccfOEJJF.o:Main.cpp:(.text+0x29): relocation truncated to fit: R_X86_64_PC32 against undefined symbol
Sally::printCrap()的 undefined reference /tmp/ccfOEJJF.o:Main.cpp:(.text+0x35):对
Sally::printCrap()'/tmp/ccfOEJJF.o:Main.cpp:(.text+0x35): relocation truncated to fit: R_X86_64_PC32 against undefined symbol
Sally::printCrap()的 undefined reference collect2:错误:ld返回1退出状态
最佳答案
您为此输入cygwin的命令必须是
g++ Sally.cpp Main.cpp
您不在命令行上包含头文件。作为建议,请查找如何使用 makefiles 。
关于c++ - 具有 undefined reference 和重定位的简单程序会被截断以适应错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25627530/