C++ C++;头文件和错误重新定位被截断以适合
在继续之前,我想告诉您,我是编程新手(虽然我做了一些python编程,但没有那么多) 我最近开始从YouTube视频和Sololearn学习C++。 我决定启动一个项目来训练自己,我认为创建一个类似python的字典类型是一个很好的项目(可能很有用) 我创建了Dictionary.h,其中包含以下代码:C++ C++;头文件和错误重新定位被截断以适合,c++,C++,在继续之前,我想告诉您,我是编程新手(虽然我做了一些python编程,但没有那么多) 我最近开始从YouTube视频和Sololearn学习C++。 我决定启动一个项目来训练自己,我认为创建一个类似python的字典类型是一个很好的项目(可能很有用) 我创建了Dictionary.h,其中包含以下代码: #ifndef DICTIONARY_H #define DICTIONARY_H class Dictionary { // Dictionary is a custom type tha
#ifndef DICTIONARY_H
#define DICTIONARY_H
class Dictionary { // Dictionary is a custom type that takes a key value(string required) and returns a value linked to it(int, string, bool are supported)
private:
std::list<std::string> keyList;
std::list<char> containerList; // I == int | S == string | B == bool
std::list<int> indexingList; // contains the index of each value
std::list<int> intVList;
std::list<std::string> strVList;
std::list<bool> boolVList;
// private function for getting the last index from a value list
int getLastIndex(char lType);
int getKeyIndex(std::string key);
char getContainerFromIndex(int indexNum);
int getIndexFromKeyIndex(int indexNum);
int _getValue(std::string key);
public:
// constructor
Dictionary();
// add element to the dictionary
void _add(std::string key, int value);
void _add(std::string key, std::string value);
void _add(std::string key, bool value);
// get element from the dictionary using the key value
int _get(std::string key);
// check if a key exists in the dictionary
bool keyInDict(std::string key);
};
#endif
#由于我的计算机语言的原因,我不得不通过谷歌翻译程序来传递输出,所以它可能与你通常看到的有所不同
顺便说一下,所有文件(Dictionary.h、Dictionary.cpp、run.cpp)都在同一个目录中
问题
我试图编辑Dictionary.h并添加构造函数体 (类似{}的空体)和第一个错误 关于Dictionary::Dictionary()没有出现(但其余的都出现了)。
我的猜测是:它不会读取Dictionary.cpp,因为如果我将main编辑为std::cout,那么在执行主文件之前,必须链接主文件和.cpp文件。一种解决方案是创建一个makefile并运行它。 或者试试这个 此行创建cpp文件的.o文件
g++ -c main.cpp Dictionary.cpp
此代码将两个.o文件链接在一起
g++ main.o Dictionary.o
我不会说
.cpp
文件需要“在主文件之前执行”。但这两者确实需要联系在一起。执行是指我需要先编译它们?然后编译并执行run.cpp?@PTorPro必须首先链接这两个文件,才能使用在其他文件中实现的函数。代码的第一行创建.o文件和第二行链接both@0x5453谢谢,伙计。这能回答你的问题吗?在任何多文件构建中,如果不使用像VisualStudio或XCode这样的IDE为您管理构建的话,您最终需要认真学习Makefile
或基于CMake的构建。手工做这件事只会带来麻烦。
$ g ++ run.cpp
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc7qwomO.o:run.cpp :(. text + 0x1e): undefined reference to `Dictionary :: Dictionary () '
/tmp/cc7qwomO.o:run.cpp:(.text+0x1e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Dictionary :: Dictionary () '
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc7qwomO.o:run.cpp :(. text + 0x58): undefined reference to `Dictionary :: _ add (std :: string, int) '
/tmp/cc7qwomO.o:run.cpp:(.text+0x58): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Dictionary :: _ add (std :: string, int) '
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc7qwomO.o:run.cpp :(. text + 0xab): undefined reference to `Dictionary :: _ add (std :: string, int) '
/tmp/cc7qwomO.o:run.cpp:(.text+0xab): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Dictionary :: _ add (std :: string, int) '
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc7qwomO.o:run.cpp :(. text + 0xf9): undefined reference to `Dictionary :: _ get (std :: string) '
/tmp/cc7qwomO.o:run.cpp:(.text+0xf9): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Dictionary :: _ get (std :: string) '
collect2: error: ld returned output mode 1
g++ -c main.cpp Dictionary.cpp
g++ main.o Dictionary.o