C++ 头文件 - 包含什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13441822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Header Files - What to include
提问by Pantelis Sopasakis
I have written a very simple class in C++, namely it is the Rectangle class from http://www.cplusplus.com/doc/tutorial/classes/. In particular here's the content of the Header file (Rectangle.h):
我用 C++ 编写了一个非常简单的类,即来自http://www.cplusplus.com/doc/tutorial/classes/的 Rectangle 类。特别是这里的头文件(Rectangle.h)的内容:
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle {
private:
double m_x;
double m_y;
public:
Rectangle();
Rectangle(double, double);
void setXY(double, double);
double getArea();
};
#endif
Here is the implementation (Rectangle.cpp):
这是实现(Rectangle.cpp):
#include "Rectangle.h"
Rectangle::Rectangle() {
setXY(1, 1);
}
Rectangle::Rectangle(double x, double y) {
setXY(x, y);
}
void Rectangle::setXY(double x, double y) {
m_x = x;
m_y = y;
}
double Rectangle::getArea(void) {
return m_x * m_y;
}
Now, I'm supposed to include the Header of Rectangle in my main class, that is:
现在,我应该在我的主类中包含 Rectangle 的 Header,即:
#include <stdlib.h>
#include <iostream>
#include "Rectangle.h"
using namespace std;
int main(void) {
Rectangle a;
cout << "Area : " << a.getArea() << "\n";
return EXIT_SUCCESS;
}
But, then I get the error:
但是,然后我收到错误:
make all
g++ -O2 -g -Wall -fmessage-length=0 -c -o Chung1.o Chung1.cpp
g++ -o Chung1 Chung1.o
Chung1.o: In function `main':
/home/chung/eclipse_ws/Chung1/Chung1.cpp:8: undefined reference to `Rectangle::Rectangle()'
/home/chung/eclipse_ws/Chung1/Chung1.cpp:9: undefined reference to `Rectangle::getArea()'
collect2: ld returned 1 exit status
make: *** [Chung1] Error 1
The error is resolved if I include the file Rectangle.cpp instead. (I'm running on Eclipse)
如果我改为包含文件 Rectangle.cpp,则错误得到解决。(我在 Eclipse 上运行)
Am I supposed to include the CPP file instead after all?
毕竟我应该包含 CPP 文件吗?
Here's my Makefile:
这是我的 Makefile:
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
OBJS = Chung1.o
LIBS =
TARGET = Chung1
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
run: $(TARGET)
./$(TARGET)
How can I modify it to compile the Rectangle class as well?
如何修改它以编译 Rectangle 类?
Solution:According to the answer from the user v154c1, it is necessary to compile individual cpp files and then include their headers in the main file or in any other file where this functionality is needed. Here's any example Makefile to do so:
解决方案:根据用户 v154c1 的回答,需要编译单独的 cpp 文件,然后将它们的头文件包含在主文件或任何其他需要此功能的文件中。这是任何示例 Makefile 这样做:
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
#List of dependencies...
OBJS = Rectangle.o Chung1.o
LIBS =
TARGET = Chung1
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
run: $(TARGET)
./$(TARGET)
采纳答案by v154c1
You are not compiling and linking the Rectangle class.
您不是在编译和链接 Rectangle 类。
Your compilation should look like:
您的编译应如下所示:
g++ -O2 -g -Wall -fmessage-length=0 -c -o Chung1.o Chung1.cpp
g++ -O2 -g -Wall -fmessage-length=0 -c -o Rectangle.o Rectangle.cpp
g++ -o Chung1 Chung1.o Rectangle.o
If you're using Makefile, then just add the Rectangle.cpp the same way you use Chung1.cpp. The same goes for any IDE you may be using.
如果您使用的是 Makefile,那么只需像使用 Chung1.cpp 一样添加 Rectangle.cpp。这同样适用于您可能使用的任何 IDE。
回答by juanchopanza
No, you are not supposed to include the .cpp
. You have to compile it, this should produce a .o
file that is then linked to the main executable. Your main is failing to find and link to this .o
file for whatever reason. Without knowing the exact compilation and linking steps you are taking it is difficult to say more.
不,您不应该包含.cpp
. 你必须编译它,这应该会产生一个.o
文件,然后链接到主可执行文件。.o
无论出于何种原因,您的主要内容都无法找到并链接到此文件。在不知道您正在采取的确切编译和链接步骤的情况下,很难多说。
回答by cowboydan
Typically the .h file is the class definition, so you are right. I do not think that you have included "Rectangle.h" in the compiler options.
通常 .h 文件是类定义,所以你是对的。我认为您没有在编译器选项中包含“Rectangle.h”。