Hiding Implementation Details

Oct 4, 2008 at 3:27am
I keep getting the following linker errors whenever I try to hide the implementation details of my class. Any assistence will be greatly appreciated:

Linker error undefined reference to 'spec::spec()'
Linker error undefined reference to 'spec::getName()'
Linker error undefined reference to 'spec::printName()'


Below are my main file, specification file and inplementation file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//mainFile.cpp 
# include <iostream>

# include "spec.h"

using namespace std;

int main()
{
    spec S;
    S.getName();
    S.printName();
    
    system( "pause" );    
}// end main 


1
2
3
4
5
6
7
8
9
10
11
//spec.h (Specification file)
class spec
{
      public:             
             spec();
             void getName();
             void printName();
      private:
              //string name; 
      
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// impl.cpp (implementation File)
# include <iostream>
# include "spec.h"
using namespace std;

spec::spec()
{ 
}

void spec::getName()
{
     
  cout << "Enter your name: ";
  getline( cin, name );                                               
  
}

void spec::printName()
{
     cout << "\nYour name is " << name;                          
}
Last edited on Oct 4, 2008 at 3:35am
Oct 4, 2008 at 6:05am
Name the impl cpp file spec.cpp and see if it works.
Oct 4, 2008 at 9:35am
Obviously, "impl.cpp" doesn't get compiled or the resulting object code doesn't get linked to the object code of main.cpp. Check your compilation/linking settings. For further help you need to tell us which development environment you use (for g++/make, you would have to compile impl.cpp with the -c flag, and then main.cpp with impl.o)

The name impl, by the way, and what you say in the heading of your post, imply the usage of the "pimpl idiom", which is not what you do. You might want to change the names to avoid confusion.
Oct 4, 2008 at 12:59pm
Thanks for your replies firedraco and exception. I renamed the file to impl.cpp but still got the linker errors.

I am using Dev C++ Ver 4.9.9.2. I also tried creating a project and including these files in it but I got a similar error.
Oct 4, 2008 at 4:52pm
You mean spec.cpp, right?
Oct 5, 2008 at 2:59am
Yes, my mistake, I changed impl.cpp to spec.cpp and still get linker errors.
Oct 5, 2008 at 6:18am
Hrm...try using #ifndef headers around your header file.

Change it so it is:

1
2
3
4
5
6
#ifndef SPEC_H
#define SPEC_H

//all the code

#endif 
Oct 5, 2008 at 8:01am
Hrm...try using #ifndef headers around your header file.

While using include guards is essential in most projects, it most certainly isn't the problem here (the error was 'undefined', not 'defined multiple times').
You must have a wrong setting in your IDE. I don't know Dev C++, but I suggest that you read the manual section on how to make a project with more than one cpp file.
Oct 6, 2008 at 12:57am
Thank you very much everyone: I was creating the project incorrectly.
After doing some research on [url]http://www.uniqueness-template.com/devcpp/[/url] I corrected the mistakes I made.
For the benefit of those who need it, below is a snippet of the instructions on how to create a project using Dev C++, and below that, is my entire code.

Step 2: Create a new project.
A "project" can be considered as a container that is used to store all the elements that are required to compile a program.
Go to the "File" menu and select "New", "Project...".
Choose "Empty Project" and make sure "C++ project" is selected.
Here you will also give your project a name. You can give your project any valid filename, but keep in mind that the name of your project will also be the name of your final executable.
Once you have entered a name for your project, click "OK".
Dev-C++ will now ask you where to save your project.

Step 3: Create/add source file(s).
You can add empty source files one of two ways:
Go to the "File" menu and select "New Source File" (or just press CTRL+N) OR
Go to the "Project" menu and select "New File".
Note that Dev-C++ will not ask for a filename for any new source file until you attempt to:
Compile
Save the project
Save the source file
Exit Dev-C++

You can add pre-existing source files one of two ways:
Go to the "Project" menu and select "Add to Project" OR
Right-click on the project name in the left-hand panel and select "Add to Project".


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// clientProgram.cpp

# include <iostream>
# include "myClass.h"

int main()
{
    myClass m;
    m.setName();
    
    std::cout << "My name is " << m.getName();
    
    system ("pause");    
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// myClass.h

# ifndef MYCLASS_H
# define MYCLASS_H

# include <string>

class myClass
{
 public:
        void setName();
        std::string getName();
 private:
         std::string name;         
};
# endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// myClassImpl.cpp

# include <iostream>
# include "myClass.h"

void myClass::setName()
{  
     name = "Jones";         
}

std::string myClass::getName()
{
     return name;            
            
}
Topic archived. No new replies allowed.