在main.cpp文件中添加.h文件时的链接器错误
我一直在阅读一些文章中,它们将.h中的类delceration分开,并在.cpp文件中删除,并且仅包含.h文件。一篇类似的文章在这里。 https://learlearn.microsoft。 com/en-us/cpp/cpp/header-files-cpp?view = msvc-170 我尝试了以下内容。
classheader.h文件
#ifndef CLASSHEADER_H
#define CLASSHEADER_H
#include <string>
using namespace std;
class animal{
public:
void set_animal_prop(string a, string b, int c);
void get_animal_prop();
private:
string name;
string food;
int age;
};
#endif
classheader.cpp文件
#include "classheader.h"
#include <iostream>
#include <string>
using namespace std;
void animal::set_animal_prop(string a, string b, int c)
{
name = a;
food = b;
age = c;
}
void animal::get_animal_prop()
{
cout <<"The animal is : " << name <<endl;
cout <<"It eats : " << food <<endl ;
cout << "Its age is : " << age <<endl;
}
和main.cpp文件,
#include "classheader.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
animal cat;
cat.set_animal_prop("Tom", "Mouse", 7);
animal horse;
horse.set_animal_prop("Max", "Grass", 18);
cat.get_animal_prop();
horse.get_animal_prop();
return 0;
}
但是当我尝试编译main.cpp文件时,它会在错误之后投掷。
undefined reference to `animal::set_animal_prop(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int)'
我想知道 如何通过包括classheader.h文件在classheader.cpp文件中定义的定义。有可能吗? PS-当我将classheader.cpp文件包含在我的main.cpp文件中时,我的程序正常工作。但是,我提到的文章显示了仅需要包括.h文件的示例。
谢谢
I have been reading some articles where they seperate the class delceration in .h and defenition in .cpp file and only the .h file is include in main.cpp file. One similar article is here.
https://learn.microsoft.com/en-us/cpp/cpp/header-files-cpp?view=msvc-170
I have tried the following.
classheader.h file
#ifndef CLASSHEADER_H
#define CLASSHEADER_H
#include <string>
using namespace std;
class animal{
public:
void set_animal_prop(string a, string b, int c);
void get_animal_prop();
private:
string name;
string food;
int age;
};
#endif
classheader.cpp file
#include "classheader.h"
#include <iostream>
#include <string>
using namespace std;
void animal::set_animal_prop(string a, string b, int c)
{
name = a;
food = b;
age = c;
}
void animal::get_animal_prop()
{
cout <<"The animal is : " << name <<endl;
cout <<"It eats : " << food <<endl ;
cout << "Its age is : " << age <<endl;
}
And main.cpp file
#include "classheader.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
animal cat;
cat.set_animal_prop("Tom", "Mouse", 7);
animal horse;
horse.set_animal_prop("Max", "Grass", 18);
cat.get_animal_prop();
horse.get_animal_prop();
return 0;
}
However when i try to compile the main.cpp file it throws following error.
undefined reference to `animal::set_animal_prop(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int)'
I wanted to know that
How can we get the definitions defined in classheader.cpp file by just including the classheader.h file. Is it even possible?
P.s -when i include classheader.cpp file in my main.cpp file, my program works fine. However the article that i mentioned shows example that only including the .h file is required.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在.h文件和.cpp文件中的类别中分开类声明时,
class.cpp和main.cpp文件中仅包含.h文件。
执行之前,需要将Class.CPP和Main.CPP均进行编译和链接。
下面的示例清除了它。
将类decleration放在classheader.h文件中,然后将类定义放在classheader.cpp文件中。将主函数放在main.cpp文件中
classheader.cpp将包括“ classheader.h”和
main.cpp还将包括“ classheader.h”
汇编:
compile classheader.cpp文件(将生成classheader.o文件)
g ++ -c myclass.cpp
compile main.cpp文件(将生成main.o文件)
g ++ -c main.cpp
link classheader.o and main.o file(在linux =&gt; a.Out文件上,将生成 /在windows =&gt; a.exe文件上是生成)
g ++ classheader.o main.o
执行程序
linux =&gt; ./ a.out
Windows =&gt; ./ a.exe
When separating the class declaration in .h file and class definition in .cpp file,
only the .h file is included in both class.cpp and main.cpp file.
Before execution, class.cpp and main.cpp both needs to be compiled and linked together.
Below example clears it.
Put Class decleration in classheader.h file and put class definition in classheader.cpp file. Put the main function in main.cpp file
classheader.cpp will include "classheader.h" and
main.cpp will also include "classheader.h"
Compilation:
Compile classheader.cpp file (classheader.o file will be generated)
g++ -c myclass.cpp
compile main.cpp file (main.o file will be generated)
g++ -c main.cpp
Link classheader.o and main.o file (on linux => a.out file will be generated / on windows => a.exe file will be generated)
g++ classheader.o main.o
Execute the program
Linux => ./a.out
Windows => ./a.exe