2

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> >)
4
  • That can't be the whole error message. Oh, and what is links.getlinks? Commented Sep 3, 2010 at 19:35
  • I think you are missing some of the error. That is just the type you are using. Can you please add the full error message. Commented Sep 3, 2010 at 19:35
  • undefined reference to test::getcont(std::basic_string<char, std::char_traits<char>, std::allocator<char> >) ) Commented Sep 3, 2010 at 19:36
  • what is links ? An instance of test? Commented Sep 3, 2010 at 19:36

1 Answer 1

8

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.