2014-10-04 76 views
0

我写了这个简单的代码:原型为XXX XXX ::不上课XXX匹配任何

main.h

#ifndef MAIN_H 
#define MAIN_H 

#include <stdio.h> 
#include <iostream> 
#include <vector> 
#include <stdlib.h> 

class Parameters 
{ 
    private: 
     std::string _hostname, _channel, _syslogs; 
     int _port; 
     std::vector<std::string> _list; 
    public: 
     std::string getHost() {return _hostname;} 
     std::string getChan() {return _channel;} 
     std::string getSys() {return _syslogs;} 
     std::vector<std::string> getList() {return _list;} 
     int getPort() {return _port;} 
}; 

#endif // MAIN_H header guard 

的main.cpp

#include "main.h" 

using namespace std; 

Parameters::Parameters (int argc, char** argv){ 
    if (argc<2 || argc>4){ 
     fprintf(stderr,"ERROR: Invalid count of parameters\n"); 
     exit(EXIT_FAILURE); 
    } 
} 

int main(int argc, char**argv) 
{ 
    Parameters parameters(argc,argv); 
    return 0; 
} 

但它赢得了不会编译。 G ++错误:

g++ -Wall -fexceptions -g -Wextra -Wall -c 
/home/pathtoproject/main.cpp -o obj/Debug/main.o 
/home/pathtoproject/main.cpp:13:1: error: prototype for 
‘Parameters::Parameters(int, char**)’ does not match any in class 
‘Parameters’ 

我正在使用G ++ 4.8(和CB 13.12 IDE)。

+0

您最终定义的所有成员函数(包括构造函数)都必须声明为类成员。 – 2014-10-04 09:51:07

回答

1

您正试图提供一个您尚未声明为类声明一部分的构造函数的实现。

您需要将此行添加到您的类的public:部分:

Parameters (int argc, char** argv); 

这是缺少原型哪个编译器是抱怨。添加这个原型将main.h中声明的构造函数中,您的将在main.cpp中定义

+0

啊当然...我很久没用过C++了..对不起愚蠢的问题,谢谢你的回答:) – Smarty77 2014-10-04 09:54:58

1

你必须构造函数的声明添加到Parameters

class Parameters 
{ 
    private: 
     // ... 
    public: 
     // ... 
     Parameters(int argc, char** argv); 
}; 

否则,编译器不知道该构造在其他翻译单元,其中只包括头存在。您的构造函数必须声明为public部分,以便它可以在main中调用。

相关问题