[英]making and including header files in c++ on linux machine from terminal
To start, I'm very new to c++ and Linux, so if you could keep that in mind when you respond that would be great :)首先,我对 C++ 和 Linux 还很陌生,所以如果你能在回复时记住这一点,那就太好了:)
I am trying to create a class in one file, that I will implement in the header of another file when I make an instance of that class.我正在尝试在一个文件中创建一个类,当我创建该类的实例时,我将在另一个文件的标题中实现该类。
My class file is我的类文件是
#include <iostream>
int main(){
class Box
{
public:
int _width;
int _length;
int _height;
};
}
I saved this as boxclass.h, but I did NOT compile it.我将它保存为 boxclass.h,但我没有编译它。 I read somewhere that when I add this to my header file, I should just save it as a text file.我在某处读到当我将它添加到我的头文件时,我应该将它保存为文本文件。
My other file, which I tried to include my box class in, is this:我试图在其中包含我的 box 类的另一个文件是:
#include <iostream>
#include "/home/cole/cpp/boxclass.h"
using namespace std;
int main()
{
Box outer{3,4,5};
Box inner{1,2,3};
Box newB = outer-inner;
cout << newB << endl;
}
When I try to compile this, I get these errors repeated many times with many different values当我尝试编译它时,这些错误会以许多不同的值重复多次
/home/cole/cpp/Boxclass.h:442:864: warning: null character(s) ignored
/home/cole/cpp/Boxclass.h:442:1: error: stray ‘\1’ in program
Can anyone explain to me whats going on?任何人都可以向我解释发生了什么事吗?
You have two definitions for the main() {}
function. main() {}
函数有两个定义。 That's not compliant for any c++ compiled code.这不符合任何 C++ 编译代码。
Further you have a local declaration of your class here:此外,您在此处有您班级的本地声明:
int main(){
class Box
{
public:
int _width;
int _length;
int _height;
};
}
You don't want this, but an out of scope declaration of class Box
appearing in a separate header file.您不想要这个,而是出现在单独的头文件中的class Box
的超出范围的声明。
I'd suppose what yo want is我想你想要的是
#include <iostream>
class Box {
public:
int _width;
int _length;
int _height;
};
int main(){
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.