2016-02-27 48 views
-5

如何将字符串传递给类中的方法?类和传递字符串作为方法的参数

代码

class Txtbin{ 
    private: 
     std::string input; 
     std::string output = "output.png"; 
     void error(); 

    public: 
     Txtbin(); 
     void run(); 
}; 

Txtbin::Txtbin(){ 

} 

void Txtbin::error(const char* str){ 
    throw std::runtime_error(str); 
} 

void Txtbin::run(){ 
    if(input == ""){ 
     error("Input file not defined"); 
    } 
} 

错误

# g++ -std=c++11 txtbin.cpp -o txtbin `pkg-config opencv --cflags --libs` 
txtbin.cpp:30:6: error: prototype for ‘void Txtbin::error(const char*)’ does not match any in class ‘Txtbin’ 
void Txtbin::error(const char* str){ 
    ^
txtbin.cpp:14:8: error: candidate is: void Txtbin::error() 
    void error(); 
     ^
+0

你甚至读过吗? – LogicStuff

+0

我是C++的新手,所以我不知道它是什么意思。也许你可以告诉我? – clarkk

+1

你声明'void error();'但是定义'void Txtbin :: error(const char * str)'?? ' –

回答

0

正如其他人提到的,您声明void error();但定义void error(const char* str);。在课堂上也在申报中放入const char* str参数。

0
prototype for ‘void Txtbin::error(const char*)’ 
does not match any in class ‘Txtbin’ 

你试图定义Txtbinvoid error(const char*)的功能,但它没有一个。

candidate is: void Txtbin::error() 

但是,它确实声明了一个没有参数的void error()函数。由于您在实现中实际使用该参数,因此您可能希望将其添加到其声明中。

0

像其他人一样,void error()不需要参数。但是,稍后您将创建具有参数的void错误(const char * str)。

class Txtbin{ 
    private: 
     string input; 
     string output = "output.png"; 

    public: 
     Txtbin(); 
     void error(const char*); //This is what you need. 
     /* void error(); THIS IS WHAT YOU HAD */ 
     void run(); 
}; 

void Txtbin::error(const char* str) 
{ 
    //Whatever 
} 
相关问题