Jump to content
  • Advertisement
Sign in to follow this  
orcfan32

Class Linker Error

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am testing using classes and here's my problem: I get a linker error with undefined reference to my method, "void AddText(std::string)". Here's my code: FSTR.cpp:
#include <iostream>
#include <string>
#include "FSTR.H"
using namespace std;



int main()
{
  FSTR FSTR_M;
  FSTR_M.AddText("test");
  cin.get();
}

FSTR.H:
#include <iostream>

class FSTR
{
    public:
        FSTR();
        ~FSTR();
        
        void AddText(std::string Text);
};

FSTR::FSTR()
{

}

FSTR::~FSTR()
{

}

void AddText(std::string Text)
{
     std::cout<<Text;
}

Any clue why? Thnks in advance.

Share this post


Link to post
Share on other sites
Advertisement
1. This isnt the reason i don't think but you should't have the class code and the definiton in the same source file, unless the methods are actually embedded in the class definition.

2. This:

void AddText(std::string Text)
{
std::cout<<Text;
}

should be...

void FSTR::AddText(std::string Text)
{
std::cout<<Text;
}

3. You should be passing the string by const reference most likely, so:

void FSTR::AddText(const std::string& Text)
{
std::cout<<Text;
}

Change your method definition to be the same as well.

The reason for the const reference is because 'const' signifies to the person using the function that the code inside the function doesn't change the string. The reference '&' means that you are not passing the string by value, which is slow, rather by name, as it where. By value is slow because a fresh string the same as the original is created, by reference is like using the original. For this the code in the method doesn't need to change, a reference is treated the same as by value.

There ya go bud,

ace

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!