为什么我的C++代码无法编译,出现未定义的引用错误
Why won't my C++ code compile, getting undefined reference error
对于我的课堂作业,我们必须使用类制作一个C++程序,然后编译它。这些如下。
Snake.h
#ifndef SNAKE_H
#define SNAKE_H
#include <iostream>
using namespace std;
// We need to include string since we need to access it
#include <string>
using std::string;
// Our declaration of our class
class Snake
{
public:
// These are our properties
string color;
double length;
bool venomous;
// Our two constructors, this is basically saying the snake is able to create these datatypes.
// It can either have a default data used or inputted data when called.
Snake()
{
}
// This one has the parameters
Snake(string color, double length, bool venomous)
{
}
// Our functions that we are declaring so we can use them in snake.cpp
// These aren't declared here because we don't have any data yet that they can use
// We also are just telling the code that they are here and that they may or
// may not be called at some point
void display();
void bite();
};
#endif
Snake.cpp
#include <iostream>
#include "Snake.h"
// These creates our data from the header file.
// Snake is seen below with the default data and then Snake with the parameters can be seen.
Snake::Snake()
{
color = "Red";
length = 12.5;
venomous = false;
}
Snake::Snake(string newColor, double newLength, bool newVenomous)
{
color = newColor;
length = newLength;
venomous = newVenomous;
}
// These initalize these functions so that they can be used when we do our dot callback in our main.
void Snake::bite()
{
cout << "This snake bites because he is hungry";
}
void Snake::display()
{
cout << "The color of the snake is: " << color;
cout << "The length of the snake is: " << length;
cout << "Is the snake venomous: " << venomous;
}
main.cpp
#include <iostream>
#include "Snake.h"
using namespace std;
int main()
{
// We create our first snake
Snake snake1;
// Now we need to call method that our object contains
snake1.display();
snake1.bite();
// UI is user input
bool UIVenomous;
string UIColor, isVenomous;
double UILength;
cout << "What color is the snake?" << endl;
cin >> UIColor;
cout << "How long is the snake? " << endl;
cin >> UILength;
cout << "Is the snake venomous? (y/n) " << endl;
cin >> isVenomous;
if (isVenomous == "y")
{
UIVenomous = true;
}
else if (isVenomous == "n")
{
UIVenomous = false;
}
// We now call our class and set our object with the variables from above
Snake snake2(UIColor, UILength, UIVenomous);
// We need to now call our methods that our object has
snake2.display();
snake2.bite();
}
我正在使用VSCode,当我尝试运行构建任务时,它给了我这个错误
C:UsersviensAppDataLocalTempcclzLMka.o: In function `main':
c:/Users/viens/OneDrive/Documents/SchoolWork/Lab3/main.cpp:10: undefined reference to `Snake::display()'
c:/Users/viens/OneDrive/Documents/SchoolWork/Lab3/main.cpp:11: undefined reference to `Snake::bite()'
c:/Users/viens/OneDrive/Documents/SchoolWork/Lab3/main.cpp:36: undefined reference to `Snake::display()'
c:/Users/viens/OneDrive/Documents/SchoolWork/Lab3/main.cpp:37: undefined reference to `Snake::bite()'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
在我删除头文件中构造函数的主体后,它会定期编译。
/* Snake.h
*/
Snake();
Snake(string color, double length, bool venomous);
如果存在未定义的引用错误,通常是因为.o
文件(从.cpp
文件创建(不存在,并且编译器/构建系统无法链接它。
您可以在CLI:g++ main.cpp other.cpp etc.cpp
中使用它
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 尝试运行 wasm 函数时出现模块未定义错误
- c++中数组的未定义错误
- 基类未定义.错误 C2504
- 获取线函数未定义错误。无法在字符串中保存可验证的内容
- 对于我的 ComplexNumber 中的某些方法,我得到了一个奇怪的未定义错误引用.cpp,不过我对模板很陌生
- 类类型重定义和基类未定义错误
- C 中的基类未定义错误
- QML[未定义]错误
- 如何构建使用 OpenCV 的 XCode 6 iOS 应用程序 - 未定义错误__cplusplus
- 变量未定义错误
- 静态方法声明但未定义错误c++
- c++ cocos-2d-x未定义错误
- 基类未定义错误(C2504)
- 单例作为模板,未定义错误
- Solaris库与STLport4.6.2链接,出现与ostream相关的未定义错误