【发布时间】:2020-09-22 11:36:50
【问题描述】:
对于我的课堂作业,我们必须使用一个类制作一个 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:\Users\viens\AppData\Local\Temp\cclzLMka.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
【问题讨论】:
-
你在 VS 代码中使用什么编译器?很可能您只是在编译 main.cpp 而不是 Snake.cpp,因此它找不到此函数的定义
-
你需要编译所有文件,而不仅仅是
main.cpp。如果您只编译main.cpp,那么#include "Snake.h"仍然可以工作(main.cpp将“看到”Snake类中的签名)并且main.cpp编译得很好,但是在生成可执行文件时,编译器需要链接所有内容在一起,在那个阶段,它找不到Snake类中方法的实现。因此,在链接过程中会出现未定义的引用错误。 -
您的错误很可能在您的
tasks.json文件中 -
Visual Studio Code 是一个棘手的问题。我不向尝试学习 C++ 的人推荐它,因为他们最终不得不学习两件事,并且在他们学会了 Visual Studio Code 的护理和喂养之后才能有效地学习重要的一件事 C++。这可能会在几年内发生变化,但现在您最好使用像 Visual Studio 这样的预配置 IDE。
标签: c++ visual-studio-code main