C++ 混淆编译器错误

C++ 混淆编译器错误,c++,compiler-construction,compiler-errors,linker,header-files,C++,Compiler Construction,Compiler Errors,Linker,Header Files,好的,如果需要的话,我会添加代码,但通常我很擅长找出错误。然而,我得到的这个是非常混乱的。我正在使用三个文件。一个驱动程序代码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_tr

好的,如果需要的话,我会添加代码,但通常我很擅长找出错误。然而,我得到的这个是非常混乱的。我正在使用三个文件。一个驱动程序代码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
g++-g-Wall-o citysim citysim.o utils.o
utils.o:在函数“城市地图::读取(标准::基本字符串)”中:
/home/rryder1/cs302/labs/lab4/utils.cpp:95:“城市地图::读取(标准::基本字符串)”的多重定义
citysim.o:/home/rryder1/cs302/labs/lab4/utils.h:61:首先在此处定义
citysim.o:在函数“城市地图::读取(标准::基本字符串)”中:
citysim.cpp:(.text+0xcb):对“city::city()”的未定义引用
citysim.cpp:(.text+0x244):对“city::~city()”的未定义引用
citysim.cpp:(.text+0x2a3):对“city::~city()”的未定义引用
citysim.o:在函数“std::vector::_M_insert_aux(__gnu_cxx::_normal_迭代器,city const&)”中:
citysim.cpp:(.text.znst6; vectori4citysais0_EE13_M_insert_auxEN9_gnu cxx17_normal_迭代器0_S2_EERKS0[std::vector:_M_insert_aux(_gnucxx:_normal_迭代器,city const&])+0xd2):未定义的“city:~city()引用”
citysim.cpp:(.text.znst6; vectori4citysais0_EE13_M_insert_auxEN9_gnu cxx17_normal_迭代器s0_S2_EERKS0[std::vector:_M_insert_aux(_gnucxx:_normal_迭代器,city const&])+0x27d):对“city:~city()的未定义引用
citysim.o:在函数“\uu gnu\u cxx::new\u分配器::destroy(city*)”中:
citysim.cpp:(.text._ZN9_uugnu_cx13new_分配器4citye7DestroyeP1.[u gnu_cxx::new_分配器::destroy(city*)]+0x18):未定义对“city:~city()”的引用
citysim.o:citysim.cpp:(.text.\u ZSt8\u destroi4cityevpt.[uvoid std::\u Destroy(city*)]+0x14):下面是对“city:~city()”的更多未定义引用
utils.o:在函数“城市地图::读取(标准::基本字符串)”中:
utils.cpp:(.text+0xacd):对“city::city()”的未定义引用
utils.cpp:(.text+0xc68):对“city::~city()”的未定义引用
utils.cpp:(.text+0xcc7):对“city::~city()”的未定义引用
collect2:ld返回了1个退出状态
它可能源于我不确定如何将utils.cpp与utils.h链接,或者是否需要链接。我习惯于在一个cpp文件中写入所有内容。utils.cpp和citysim.cpp都有utils.h的include语句。h基本上只有类定义和函数头。utils.cpp包含所有函数定义,citysim.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
\ifndef\uuu CITY\uu DEFS\H__
#定义城市定义__
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//#包括此头文件所需的所有头文件
阶级城市{

朋友OsFrand和运算符< P>这不是编译器错误,它是链接错误。对于链接的“城市”类,没有一个构造函数或析构函数。确保在运行C++命令时既包含CPP文件,也可以在编译后分离出链接阶段。

构建一个C++项目涉及几个阶段。两个重要阶段是“编译”和“链接”。编译器采用每个C++文件并生成一个“对象”文件。对象文件包含所有定义的符号(类、函数、方法、全局变量等)。。对象文件的某些部分可能引用原始对象文件中不存在的其他符号

在本例中,您将拥有
util
对象文件,该文件引用
city::~city()
city::city()

在编译阶段,源代码还可以引用另一个对象中的符号。通常使用.h文件提供有关符号的信息,但通常不包括实际符号本身。在您的示例中,
util.h
将“此程序作为城市构造函数和析构函数”,但实际上并没有提供


正如您在评论中所说,您确实需要将它们添加到“util.cpp”中,以提供链接器可以找到的实际实现。

需要更多信息来解决根本问题。我可以猜一下,您是否可以将
城市地图
的主体放在标题中?听起来您可能需要(除其他外)在
utils.h
文件中使用include-guard。基本上,使用
#ifdef
可以防止文件被多次包含。我可以想象,用多个定义解决您的问题。好的,如果有助于缩小问题的范围,我添加了头文件。如果您能解释,我将非常感谢您的帮助有什么不对劲,而不是只告诉我修复方法。虽然我会采取修复方法,但你能进一步解释一下吗。我从来没有真正处理过链接,所以我不知道我在做什么。我在utils.h文件中有一个构造函数和解构器。它是否希望我也在utils.cpp中定义它们。我猜你可能需要看看我的代码需要确定。我正在使用提供给我的make文件进行编译(这是一个类项目,但我想了解我自己使用的实际情况)。我添加了更多细节。希望这能有所帮助。