C++ C++;对错误的未定义引用(Visual Studio代码)
这是我遇到的最奇怪的问题,一切都是正确的,经过仔细检查,程序仍然没有运行。我也在一个在线IDE(zybooks;我大学的课程)上尝试了这段代码,它在那里运行得非常好 我做错了什么 main.cppC++ C++;对错误的未定义引用(Visual Studio代码),c++,visual-studio-code,undefined,C++,Visual Studio Code,Undefined,这是我遇到的最奇怪的问题,一切都是正确的,经过仔细检查,程序仍然没有运行。我也在一个在线IDE(zybooks;我大学的课程)上尝试了这段代码,它在那里运行得非常好 我做错了什么 main.cpp #include <iostream> #include "Character.h" using namespace std; int main(){ Character character("ekko", "black&qu
#include <iostream>
#include "Character.h"
using namespace std;
int main(){
Character character("ekko", "black", 100, 100);
cout << character.getName();
return 0;
}
VisualStudio代码只是编辑器编码环境,所以您需要为它添加更多的编译工具,例如Cmake来执行代码。
.
g++tempCodeRunnerFile.cpp-o tempCodeRunnerFile
main.cpp缺失。问题中没有tempCodeRunnerFile.cpp。您可能希望不再使用code runner,而是阅读有关如何修改tasks.json以构建所有源文件的文档:我建议使用构建系统而不是修改tasks.json,但这显然是主观的。
#include "Character.h"
#include <iostream>
Character::Character(){
name = "Summoner";
race = "NONRACIAL";
level = 0;
health = 0;
}
Character::Character(string name, string race, int level, int heatlh ){
this->name = name;
this->race = race;
this->level = level;
this->health = health;
}
void Character::setName(string name){ this->name = name; }
void Character::setRace(string race){ this->race = race; }
void Character::setLevel(int level){ this->level = level; }
void Character::setHealth(int health){ this->health = health; }
string Character::getName(){ return name; }
string Character:: getRace(){ return race; }
int Character:: getLevel(){ return level; }
int Character:: getHealth(){ return health; }
#include <iostream>
using namespace std;
#ifndef CHARACTER_H
#define CHARACTER_H
class Character{
private:
string name;
string race;
int level, health;
public:
Character();
Character(string name, string race, int level, int heatlh );
string getName();
string getRace();
int getLevel();
int getHealth();
void setName(string name);
void setRace(string race);
void setLevel(int level);
void setHealth(int health);
};
#endif
[Running] cd "c:\Users\diono\OneDrive\Desktop\COSC_1430_RPG_GAME\Character\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\diono\OneDrive\Desktop\COSC_1430_RPG_GAME\Character\"tempCodeRunnerFile
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:\temp\gcc\build-mingw-w64\mingw-w64-crt/../../src/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
[Done] exited with code=1 in 0.883 seconds