2014-10-01 2 views
0

C++에서 처음으로 여러 파일을 사용하려고합니다. 여기 제가 작성한 파일이 있습니다.Class :: Class() 및 Class :: function()에 대한 정의되지 않은 참조

파일 # 1 : Box.hpp

#ifndef BOX_HPP 
#define BOX_HPP 

class Box 
{ 
    private: 
     int length; 
     int width; 
     int height; 

     Box() {} 

    public: 
     Box(int _length, int _width, int _height); 

     void set_dimensions(int _length, int _width, int _height); 
     int volume(); 
}; 

#endif 

파일 # 2 : Box.cpp

#include "Box.hpp" 

Box::Box(int _length, int _width, int _height) 
{ 
    set_dimensions(_length, _width, _height); 
} 

void Box::set_dimensions(int _length, int _width, int _height) 
{ 
    length = _length; 
    width = _width; 
    height = _height; 
} 

int Box::volume() 
{ 
    return length*width*height; 
} 

파일 # 3 : MAIN.CPP

#include "Box.hpp" 
#include <iostream> 

int main() 
{ 
    Box box1 = Box(1,2,3); 
    std::cout << box1.volume() << std::endl; 
    return 0; 
} 

내가하려고 main.cpp를 실행하십시오. 다음 오류가 발생합니다 :

'상자 : 상자 (int, int, int)를' '상자 :: 볼륨()'에

정의되지 않은 참조에 16,

정의되지 않은 참조는

그 이유를 알아낼 수 없습니다. 나는 당신이 이런 식으로 컴파일하는 생각

$ g++ main.cpp Box.cpp 

:

+2

? 두 * 소스 파일을 모두 연결하고 있습니까? –

+0

아마도 도움이 될 것입니다. http://stackoverflow.com/questions/14173217/c-calling-a-function-from-another-class – Matheno

답변

1

당신처럼 두 파일을 사용하여 컴파일 할 필요가 어떻게 프로그램을 구축하고있다

$ g++ main.cpp 
관련 문제