2012-04-28 111 views
2

我想分解一个程序,我已经写入两个源文件,但我有问题链接它们,错误信息对我没有任何意义。链接源文件

我会提供的文件的缩短版相关信息:

//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,但我得到的错误信息,当我把它分解了描述。为了测试目的,我添加了“添加”功能,并且编译好。

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

它清楚地标记为字​​符指针返回类型,并且在没有从grandiose.cpp中分离出来时工作,所以我对这里的问题非常困惑。

编辑:

好的,移动使用命名空间std后;到适当的地方,并包括标头警卫推荐这些错误信息消失,但我正在得到新的,我进一步困惑。我将包含完整的文件和错误消息。

错误消息: province.obj:错误LNK2005: “市民:__thiscall省::省(无效)”(?? 0province @@ QAE @ XZ)在grandiose.obj已经定义

province.obj :error LNK2005:“public:__thiscall province :: province(class std :: basic_string,class std :: allocator>,int * const)”(?? 0province @@ QAE @ V?$ basic_string @ DU?$ char_traits @ D @性病@@ V'$ @分配器@ 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; 
    } 

回答

0

就像一个风格问题,当您启动包括多个文件,你应该把一个预处理器在后台顶部的头文件,以防止它们被包含两次并导致解析错误。举例来说,你应该把所有的标题下面:

//first thing at the top of the header 
#ifndef THINGY_H 
#define THINGY_H 

//...the actual header code 

//the very last line of the header 
#endif //THINGY_H 

这样,如果一个代码模块的编译你结束了一个头文件被包含两次时,则预处理器将已经拥有的头后卫令牌被定义,并且因此将不包括来自头的第二副本的代码。

+0

我不认为这是一个头部卫队问题:没有钻石/递归包括在这里造成问题 – Attila 2012-04-28 17:21:13

+0

是的,这是一个错字,它是#include“thingy.h”在文件中。 – 2012-04-28 17:29:02

2

即使你有<string>,你还需要使用std命名空间访问string类型:

#include<string> 
using namespace std; 

类型string驻留在std命名空间,所以只是打字string不会命名一个已知类型。

或者,你可以在头与std::string取代的string所有occurrances并在.cpp使用using声明(其实这是做事情,因为它避免了所有std功能的类型poisioning全局命名空间的期望的方式。

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

0

将您的实现移至头文件中的cpp文件或内联实现的构造函数/方法来解析链接器错误。

+0

我不明白你的意思,你能否详细说明一下? – 2012-04-28 17:54:01

+0

为省级的构造函数实现的头文件。如果您在前面加上* inline *关键字,则问题将得到解决。 – Mohammad 2012-04-28 17:58:12

+0

工作,非常感谢! – 2012-04-28 18:00:55