我已经环顾四周一个多小时了,我找不到任何对我的情况有利的东西。我是 C++ 的初学者,这是我第一次尝试将我的代码拆分为两个 .cpp 文件和一个 .h 文件。我还没有遇到任何成功。这是我的代码:
#include "stdafx.h"
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
Cat Frisky; // declare variable of type Cat, creates an object
Frisky.SetAge( 5 ); // put value INTO object
Frisky.Meow( ); // make object do something
std::cout << "Frisky is a cat who is " ;
std::cout << Frisky.GetAge() << " years old.\n " ;
Frisky.Meow( ); // make object do something
return 0;
}
// This is my cat.h
class Cat {
public:
int GetAge(); // accessor
void SetAge( int age ); // accessor
void Meow(); // general function
private:
int itsAge; // member variable
};
#include "Cat.h"
int Cat::GetAge( )
{
return itsAge;
} // end function GetAge
// set function, PUT VALUE IN to the object
void Cat::SetAge( int age )
{
itsAge = age ;
} // end function SetAge
// What do Cats do?
// What action should a Cat object perform?
void Cat::Meow( )
{
std::cout << "Meow.\n";
} // end function Meow
这是我一个多小时以来一直遇到的错误消息。
1>------ Build started: Project: TestCat, Configuration: Debug Win32 ------
1> TestCat.cpp
1>TestCat.obj : error LNK2019: unresolved external symbol "public: int __thiscall Cat::GetAge(void)" (?GetAge@Cat@@QAEHXZ) referenced in function _main
1>TestCat.obj : error LNK2019: unresolved external symbol "public: void __thiscall Cat::Meow(void)" (?Meow@Cat@@QAEXXZ) referenced in function _main
1>TestCat.obj : error LNK2019: unresolved external symbol "public: void __thiscall Cat::SetAge(int)" (?SetAge@Cat@@QAEXH@Z) referenced in function _main
1>C:\Documents and Settings\Geena\Desktop\TestCat\Debug\TestCat.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我将 Cat.cpp 文件和 Cat.h 文件放入以下目录: C:\Documents and Settings\Geena\Desktop\TestCat\TestCat
只是在寻找让这个该死的程序启动并运行的答案。
谢谢
最佳答案
我强烈建议您休假 this下一个问题之前的教程
目录.h
class Cat {
public:
int GetAge();
void SetAge( int age );
void Meow();
private:
int itsAge;
};
猫.cpp
#include "cat.h"
#include <iostream>
using namespace std;
int Cat::GetAge(){return itsAge;}
void Cat::SetAge(int age){itsAge = age ;}
void Cat::Meow(){cout << "Meow.\n";}
main.cpp
#include "cat.h"
#include <iostream>
using namespace std;
int main()
{
Cat Frisky;
Frisky.SetAge(5);
Frisky.Meow();
cout << "Frisky is a cat who is " ;
cout << Frisky.GetAge() << " years old.\n " ;
Frisky.Meow( );
system("pause");
return 0;
}
关于c++ - 我遇到了可怕的 LNK2019 错误,不知道从这里开始。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14995742/