好吧,我现在想知道为什么这段C ++代码不想编译:
的main.cpp
#include <iostream>
#include "box.h"
int main()
{
std::cout << "Hello World !" << std::endl;
Box *bx = new Box(4,8,40,40);
bx->hello();
return 0;
}
box.h
#ifndef BOX
#define BOX
class Box
{
public:
Box(int x, int y, int w, int h);
void hello();
private:
int box_x;
int box_y;
int box_w;
int box_h;
};
#endif
box.cpp
#include "box.h"
Box::Box(int x,int y, int w, int h)
{
box_x = x;
box_y = y;
box_w = w;
box_h = h;
}
void Box::hello()
{
std::cout << "Hello from C++ !" << std::endl;
}
编译器一直说:
/tmp/ccXu8pjM.o:在函数
main': main.cpp:(.text+0x59): undefined reference to
Box :: Box(int,int,int,int)&#39; main.cpp :(。text + 0x69):未定义引用`Box :: hello()&#39; collect2:错误:ld返回1退出状态 编译失败。