raj
raj

Reputation: 45

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.cpp

#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> >)

Upvotes: 2

Views: 173

Answers (1)

Tom
Tom

Reputation: 45174

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;
}

Upvotes: 8

Related Questions