0

Ok 코드를 추가해야하지만 일반적으로 오류를 파악할 때는 괜찮습니다. 그러나 내가 가진이 것은 매우 혼란 스럽습니다. 나는 3 개의 파일을 사용하고있다. 드라이버 코드 citysim.cpp와 두 개의 지원 파일 utils.cpp와 utils.h. 나는 그것들을 조정할 때 나는 잘못된 것을하고 있다는 사실로부터 유래한다고 생각한다.혼란스러운 컴파일러 오류

g++ -g -Wall -o citysim citysim.o utils.o 
utils.o: In function `map_of_cities::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': 
/home/rryder1/cs302/labs/lab4/utils.cpp:95: multiple definition of `map_of_cities::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' 
citysim.o:/home/rryder1/cs302/labs/lab4/utils.h:61: first defined here 
citysim.o: In function `map_of_cities::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': 
citysim.cpp:(.text+0xcb): undefined reference to `city::city()' 
citysim.cpp:(.text+0x244): undefined reference to `city::~city()' 
citysim.cpp:(.text+0x2a3): undefined reference to `city::~city()' 
citysim.o: In function `std::vector<city, std::allocator<city> >::_M_insert_aux(__gnu_cxx::__normal_iterator<city*, std::vector<city, std::allocator<city> > >, city const&)': 
citysim.cpp:(.text._ZNSt6vectorI4citySaIS0_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS0_S2_EERKS0_[std::vector<city, std::allocator<city> >::_M_insert_aux(__gnu_cxx::__normal_iterator<city*, std::vector<city, std::allocator<city> > >, city const&)]+0xd2): undefined reference to `city::~city()' 
citysim.cpp:(.text._ZNSt6vectorI4citySaIS0_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS0_S2_EERKS0_[std::vector<city, std::allocator<city> >::_M_insert_aux(__gnu_cxx::__normal_iterator<city*, std::vector<city, std::allocator<city> > >, city const&)]+0x27d): undefined reference to `city::~city()' 
citysim.o: In function `__gnu_cxx::new_allocator<city>::destroy(city*)': 
citysim.cpp:(.text._ZN9__gnu_cxx13new_allocatorI4cityE7destroyEPS1_[__gnu_cxx::new_allocator<city>::destroy(city*)]+0x18): undefined reference to `city::~city()' 
citysim.o:citysim.cpp:(.text._ZSt8_DestroyI4cityEvPT_[void std::_Destroy<city>(city*)]+0x14): more undefined references to `city::~city()' follow 
utils.o: In function `map_of_cities::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': 
utils.cpp:(.text+0xacd): undefined reference to `city::city()' 
utils.cpp:(.text+0xc68): undefined reference to `city::~city()' 
utils.cpp:(.text+0xcc7): undefined reference to `city::~city()' 
collect2: ld returned 1 exit status 

그것은 내가 utils.h 또는 내가 필요하면 utils.cpp을 연결하는 방법을 잘 모르겠습니다이다에서 줄기 수 한 가지를 다음과 같이

오류입니다. 나는 하나의 cpp 파일에 모든 것을 쓰는 데 익숙하다. utils.cpp와 citysim.cpp에는 모두 utils.h에 대한 명령문이 있습니다. utils.h는 기본적으로 클래스 정의와 함수 헤더를 가지고 있습니다. utils.cpp는 모든 함수 정의를 포함하고 citysim.cpp는 main 함수를 포함합니다. 나는 내가 할 수있는 다른 파일을 포함 할 필요가 있지만, utils.cpp 꽤 큰 단지 가려 낼 수있는 엉망이 될 경우

여기 내 utils.h 파일

#ifndef __CITY_DEFS_H__ 
#define __CITY_DEFS_H__ 

#include <string> 
#include <fstream> 
#include <iostream> 
#include <cstdlib> 
#include <algorithm> 
#include <cmath> 
#include <vector> 
#include <iomanip> 
using namespace std; 

//#include ALL HEADER FILES NEEDED FOR THIS HEADER FILE 

class city { 
    friend ostream &operator<<(ostream &, const city &); 
    //OTHER FRIENDS 
    friend class map_of_cities; 
    public: 
    city(); 
    ~city(); 

    // MEMBER FUNCTIONS 
    protected: 
    // MEMBER DATA 

    string Name, type; 
    float latitude, longitude; 
    int population, listlocation; 


    // MEMBER FUNCTIONS 
}; 


ostream &operator<<(ostream &, const city &); 

//OTHER CLASS DEFINITIONS 
class map_of_cities { 
    public: 
     map_of_cities(); 
     ~map_of_cities(); 

     void read(string fname); 
     float distance(city one, city two); 
     void populatelists(); 
     void print(); 
     int listpop(vector<city> list); 

    private: 
     vector<city> cities; 
     vector<city> local; 
     vector<city> regional; 
     vector<city> global; 
}; 

#endif 

입니다. 내 문제는 이것이야?

+0

기본 문제를 해결하려면 추가 정보가 필요합니다. –

+0

'map_of_cities'의 본문을 헤더에 넣을 수 있습니까? – chris

+0

'utils.h' 파일에 include-guard를 사용할 필요가있는 것처럼 들릴 수도 있습니다. 기본적으로, 파일이 두 번 이상 포함되는 것을 막는'# ifdef '를 사용하는 트릭입니다. 여러 상상의 문제로 문제를 해결하십시오. – enhzflep

답변

1

이것은 컴파일러 오류가 아니며 링크 오류입니다. 연결된 "도시"클래스의 생성자 또는 소멸자가 없습니다. C++ 명령을 실행할 때 두 cpp 파일을 모두 포함 시키거나 컴파일 한 후에 연결 단계를 분리하십시오.

C++ 프로젝트를 작성하는 과정은 여러 단계로 이루어집니다. 중요한 두 단계는 "컴파일"과 "링크"입니다. 컴파일러는 각 C++ 파일을 가져 와서 "개체"파일을 생성합니다. 오브젝트 파일에는 정의 된 모든 기호 (클래스, 함수, 메소드, 전역 변수 등)가 들어 있습니다. 개체 파일의 일부가 원래 개체 파일에없는 다른 기호를 참조 할 수 있습니다.

귀하의 경우 city::~city()city::city()을 나타내는 util 개체 파일이 있습니다.

컴파일 단계에서 소스 코드는 다른 개체에 포함될 기호를 참조 할 수도 있습니다. 일반적으로 .h 파일은 기호에 대한 정보를 제공하는 데 사용되지만 일반적으로 실제 기호 자체는 포함하지 않습니다. 귀하의 경우에 util.h은 "이 프로그램을 도시 생성자 및 소멸자"라고 지정하지만 실제로는 을 제공하지 않습니다.

귀하의 의견에서 말했듯이 실제로 "util.cpp"에 추가해야 링커에서 찾을 수있는 실제 구현이 제공됩니다.

+0

죄송합니다. 조금 더 설명해 주실 수 있습니까? 필자는 실제로 링크를 다루지 않았으므로 내가하는 일에 단서가 없다. utils.h 파일에 생성자와 deconstructor가 있습니다. utils.cpp에서 그것들을 정의하기를 기대하고 있습니까? 당신이 추측하면 내 코드를 확실히 알아야 할 필요가 있을지도 모릅니다. 나에게 제공되는 컴파일 파일을 사용하고있다. (이것은 클래스 프로젝트를위한 것이지만, 실제로는 내 용도로 사용되는 것을 배우는 것과 같다.) –

+0

자세한 내용을 추가했습니다. 잘하면 도움이됩니다. – Daniel