混淆编译器错误
Confusing compiler error
确定如果需要,我会添加代码,但通常我很体面地弄清楚错误。但是我得到的这是非常令人困惑的。我正在使用三个文件。驱动程序代码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.cpp与utils.h联系起来,或者如果需要的话。我习惯于将所有内容写入一个CPP文件中。utils.cpp和citysim.cpp都包含utils.h的语句。utils.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
如果我需要包括我可以的其他文件,但是utils.cpp非常大,只是一团糟。我的问题是吗?
这不是编译器错误,而是链接错误。您没有链接的"城市"课程的构造函数或驱动器。运行C 命令时,请确保将两个CPP文件都包含在编译后的链接阶段。
。构建C 项目涉及几个阶段。两个重要阶段是"编译"answers"链接"。编译器获取每个C 文件并生成一个"对象"文件。该对象文件包含所有定义的符号(类,功能,方法,全局变量等)。对象文件的一部分可能会引用原始对象文件中不在原始对象文件中的其他符号。
在您的情况下,您将拥有util
对象文件,其中引用city::~city()
和city::city()
。
在编译阶段,源代码还可以引用将在另一个对象中的符号。通常,.h文件用于提供有关符号的信息,但通常它们不包含实际符号本身。在您的情况下, util.h
阐明了"该程序作为城市构造函数和驱动器",但实际上并未提供。
正如您在评论中所说的,您确实需要在" util.cpp"中添加它们,以提供链接器可以找到的实际实现。
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 致命错误 C1001 :vs2017 15.8.4 的内部错误(编译器文件"msc1.cpp",第
- C++2440错误-编译器认为字符串是常量字符
- Clang 错误 – 编译器错误或缺少一些细节?
- 视觉C++ 2017 错误?编译器优化表达式
- 配置:错误:C++编译器无法创建可执行文件
- PHP PDO_ODBC配置:错误:C 编译器无法创建可执行文件
- 致命错误 C1001:内部编译器错误(编译器文件"MSC1.cpp",第 1794 行)
- 错误:编译器限制达到简单hello world程序的堆限制
- c++链接错误:编译器找不到函数的定义
- 生成 CImg 时出现致命错误(编译器限制)
- 抛出对齐类型时出现 Clang 运行时错误.编译器错误