开发者

class method error in c++

I am getting an error when I declare a class:

#include <iostream>
#include "testing/test.h"
#include <string>
using namespace std;

int main(void)
{    
    test links;
    string content="this is an string";
    links.getcont(content);
}

test.h

#ifndef TEST_H_
#define TEST_H_
#include<string>
using namespace std;
class test {    
  public:    
    string getcont(string content); 

};

#endif /* TEST_H_ */

test.c开发者_如何学JAVApp

#include "test.h"
#include <iostream>
using namespace std;    
string getcont(string content)    
{
    cout << content;
    return content;
}

When I run this I get this error:

undefined reference to test::getcont(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)


Well , in your test.cpp file replace the getcont function for this

string test::getcont(string content){ //code here; }

The problem is that you are not saying that getcont is a member function of the test class.

Also, consider making it a const function and passing a const string reference

string
test::getcont( const string& content) const
{
    return content;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜