c++ - 链接源文件

标签 c++

我正在尝试将已编写的程序分解为两个源文件,但在链接它们时遇到问题,并且错误消息对我来说没有任何意义。

我将提供包含相关信息的文件的缩短版本:

//grandiose.cpp:

#include "thingy.h"

int main() {}

//thingy.h:
#include<string>
int add (int x, int y);
char * parse_input_fragment(const string & ,const string & , size_t &, size_t &);

//thingy.cpp:
#include "thingy.h"

int add (int x, int y)
{
    return x+y;
}

char * parse_input_fragment(const string & objective,const string & input, size_t & first_finder, size_t & second_finder) {
    char * string_to_int_buffer = new char[64];
    first_finder = input.find(objective, first_finder);
    first_finder = (input.find('=', first_finder))+1;
    second_finder = input.find(';', first_finder);
    int y = 0;
    for(unsigned int x = first_finder; x < second_finder; x++) {
        if (  (input[x] != ' ') && (input[x] != '\n') ) {
            string_to_int_buffer[y] = input[x];
            y++;
        }
    }
    string_to_int_buffer[y] = '\0';
    first_finder = second_finder;
    return string_to_int_buffer;
}

如果我将 parse_input_fragment 函数放入 grandiose.cpp 中,它可以正常编译,但是当我按照描述将其拆分时,我会收到错误消息。我出于测试目的添加了“add”函数,该函数编译得很好。

我收到的错误消息是针对 thingy.h 中的原型(prototype): 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持default-int

它被明确标记为字符指针返回类型,并且在不从 grandiose.cpp 中拆分出来时可以工作,所以我对这里的问题感到非常困惑。

编辑:

好的,移动 using 命名空间 std 后;到适当的位置并按照建议包含 header 防护装置,这些错误消息消失了,但我收到了新的消息,我对此感到更加困惑。我将包含完整的文件和错误消息。

错误消息: Province.obj:错误LNK2005:“public:__thiscall省::province(void)”(??0province@@QAE@XZ)已在grandiose.obj中定义

province.obj:错误LNK2005:“公共(public):__thiscall省::省(class std::basic_string,class std::allocator>,int * const)”(??0province@@QAE@V?$basic_string@ DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAH@Z) 已在 grandiose.obj 中定义

    // grandiose.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <vector>
    #include <ctime>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "market.h"

    int _tmain(int argc, _TCHAR* argv[])
    {
        market England;
        ifstream reader;
        reader.open ("provinces.txt", ios::in);
        if (reader.is_open()) {  //check if txt file successfully opened
            cout << "\n\nprovinces.txt was successfully opened.\n\n";
        }
        else {
            cout << "\n\nfile was not successfully opened.\n\n";
            return (1);
        }
        string reader_buffer;
        while ( reader.good() ) { // while not end of file
            getline (reader, reader_buffer, '}'); // get one province's worth of data from txt file
            if (!reader_buffer.empty()) {
                parse_provinces (reader_buffer); // send data to be parsed
            }
        }
        add(1,2);
        return 0;
    }


    //market.h

    #include <iostream>
    #include <list>
    #include "province.h"
    using namespace std;

    class market
    {
        public:
        list<province> provinces;
    };

    //province.h

    #ifndef PROVINCE_H
    #define PROVINCE_H
    #include <iostream>
    #include <string>

    using namespace std;

    int add (int x, int y);
    char * parse_input_fragment(const string & ,const string & , size_t &, size_t &);

    class province
    {
    public:
        province::province();
        province::province(string, int[]);
        unsigned int * rural_poor;
        unsigned int * urban_poor;
        unsigned int * max_mine_jobs;
        unsigned int * max_farm_jobs;
        unsigned int * employed_mine;
        unsigned int * employed_farm;
        unsigned int * employed_factory;
        string name;
    };
    province::province () {
        rural_poor = new unsigned int(0);
        urban_poor = new unsigned int(0);
        max_mine_jobs = new unsigned int(0);
        max_farm_jobs = new unsigned int(0);
        employed_mine = new unsigned int(0);
        employed_farm = new unsigned int(0);
        employed_factory = new unsigned int(0);
        name = "";
    }
    province::province (string name, int numbers[]) {
        province::name = name;
        cout << "This province is named " << province::name << endl;
        rural_poor = new unsigned int(numbers[0]);
        cout << "Rural poor = " << *rural_poor << endl;
        urban_poor = new unsigned int(numbers[1]);
        cout << "Urban poor = " << *urban_poor << endl;
        max_mine_jobs = new unsigned int(numbers[2]);
        cout << "Max mine jobs = " << *max_mine_jobs << endl;
        max_farm_jobs = new unsigned int(numbers[3]);
        cout << "Max farm jobs = " << *max_farm_jobs << endl;
    }

    province * parse_provinces(string);

    #endif

    //province.cpp

    #include "stdafx.h"
    #include "province.h"

    int add (int x, int y)
    {
        return x+y;
    }

    char * parse_input_fragment(const string & objective,const string & input, size_t & first_finder, size_t & second_finder) {
        char * string_to_int_buffer = new char[64];
        first_finder = input.find(objective, first_finder);
        first_finder = (input.find('=', first_finder))+1;
        second_finder = input.find(';', first_finder);
        int y = 0;
        for(unsigned int x = first_finder; x < second_finder; x++) {
            if (  (input[x] != ' ') && (input[x] != '\n') ) {
                string_to_int_buffer[y] = input[x];
                y++;
            }
        }
        string_to_int_buffer[y] = '\0';
        first_finder = second_finder;
        return string_to_int_buffer;
    }

    province * parse_provinces(string input) {
        size_t first_finder;
        size_t second_finder;
        char * string_to_int_buffer;
        int population_info[4];


        // find the name
        first_finder = input.find('=');
        string name;
        for(unsigned int x = 0; x < first_finder; x++) {
            if ((input[x] != ' ') && (input[x] != '\n')) {
                name.push_back(input[x]);
            }
        }

        // find the rural poor
        string_to_int_buffer = parse_input_fragment("rural_poor", input, first_finder, second_finder);
        population_info[0] = atoi(string_to_int_buffer);

        // find the urban poor
        string_to_int_buffer = parse_input_fragment("urban_poor", input, first_finder, second_finder);
        population_info[1] = atoi(string_to_int_buffer);

        // find max mine jobs
        string_to_int_buffer = parse_input_fragment("max_mine_jobs", input, first_finder, second_finder);
        population_info[2] = atoi(string_to_int_buffer);

        // find max farm jobs
        string_to_int_buffer = parse_input_fragment("max_farm_jobs", input, first_finder, second_finder);
        population_info[3] = atoi(string_to_int_buffer);

        delete[] string_to_int_buffer;
        string_to_int_buffer = NULL;
        province * current_province = new province(name, population_info);
        return current_province;
    }

最佳答案

即使您包含 <string> ,您还需要使用std命名空间来访问 string类型:

#include<string>
using namespace std;

类型string驻留在 std 命名空间中,因此只需输入 string不会命名已知类型。

或者,您可以替换所有出现的 stringstd::string在标题中并使用 using .cpp 中的语句(实际上这是理想的处理方式,因为它避免了所有 std 函数和类型对全局命名空间造成污染。

Jason 的回答也是一个很好的建议:使用包含保护来保护你的头文件,因此如果你两次包含相同的头文件(例如间接),你将不会因为多个定义而出现编译器错误(C++ 有一个单定义规则) :所有类只能定义一次)。在您的具体情况下,这不是问题,但养成这样做的习惯以避免以后的问题是一件好事

关于c++ - 链接源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10365704/

相关文章:

c++ - MongoDb 总和聚合 C++

c++ - 使用 C/C++ 在 Ubuntu 中查找 Arduino 连接到哪个 TTY 端口

c++ - 在 Visual Studio 调试器中查看 C++ 智能指针数组的内容?

c++ - FFT 与 DFT 有何不同?如何用 C++ 实现它们?

c++ - 为什么 std::bitset<8> 是 4 个字节大?

c++ - 为什么这个 COM IUnknown::Release 的实现有效?

c++ - 如何设置类的 char* 成员?使用 strcat 或其他东西?

c++ - Windows 64 位上的 fftw3

c++ - 将 time_t 转换为 int

c++ - 如何在 GCC 2.96 中分解 C++