Tutorial 42 - undefined reference

+3 Douglas Jr Crawshaw · January 6, 2016
Hello again.  I'm at the 42nd tutorial, and I have this code:

main.cpp

#include "sally.h"
#include <iostream>
using namespace std;

int main()
{
    sally sallyObject;

    sallyObject.printCrap();
    return 0;
}

sally.h

#ifndef SALLY_H
#define SALLY_H


class sally
{
    public:
        sally();
        void printCrap();
    protected:
    private:
};

#endif // SALLY_H


sally.cpp

#include "sally.h"
#include <iostream>
using namespace std;

sally::sally()
{

}


void sally::printCrap()
{
    cout << "Crap\n";
}


And I get the following errors:



undefined reference to 'sally::sally()'
undefined reference to 'sally::printCrap()'


I think it's still a console application, and the three files are the only ones in the project.  Was there something I missed?  I didn't include the pointer line; I got the error regardless.

Post a Reply

Replies

Oldest  Newest  Rating
0 Tim Brelsford · January 7, 2016
Looks right.  I just ran it.  If your using Visual Studio you have to add #include #include "stdafx.h"

Here is mine.

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>
#include "sally.h"

int main()
{
system("color 8b");

sally sallyObject;
sallyObject.printCrap();



system("pause");
    return 0;
}


#include "stdafx.h"
#ifndef  SALLY_H
#define SALLY_H
#include<iostream>
using namespace std;

class sally
{
public:
sally();
void printCrap();


};



#endif // ! SALLY_H


#include "stdafx.h"
#include "sally.h"


using namespace std;
sally::sally()
{

}

void sally::printCrap()
{
cout << "print crap" << endl;
}
0 Tim Brelsford · January 7, 2016
It could also be the sally.cpp may not be in the same folder as the sally.h file.  
0 Douglas Jr Crawshaw · January 7, 2016
OK, the same thing happened to me again.  sally.cpp was indeed part of the project, but so was the old burrito class.  I deleted it, and boom, it worked.  Any reason why CodeBlocks would act that way?
0 first last · January 8, 2016
" I deleted it, and boom, it worked. Any reason why CodeBlocks would act that way?"

too difficult to tell without seeing your workspace / project 

you can have multiple projects in the same workspace 

so you could have added the file to the wrong project 

you could have had a different project set as your start up project 

----- 
you only need 
#include "stdafx.h 
if the VS compiler is set to use precompiled headers 

stdafx.h is some non-standard MS stuff 
don't use it 
it just makes life easier not to 

I don't know why Krootushas Gesu thinks codeblocks breaks as usual -- codeblocks is awesome -- it never breaks for me 

its just like everything is with coding - garbage in, garbage out -- you just have to use it properly 

"the final solution to this problem is to reinstalling the IDE."

no, the final solution is to fix your project 

there is nothing wrong with codeblocks  
  • 1

C++

107,087 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator