Click to See Complete Forum and Search --> : newbie problem with multiple file implementation


parsley
November 30th, 2003, 08:02 PM
hi, i'm trying to use multiple file implementation with classes for a roman numeral problem. i have three files:

main.cpp (driver program)
RomanType.cpp
RomanType.h

i get this error when trying to build the main.cpp file:

Linking...
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall RomanType::SetRoman(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?SetRoman@RomanType@@QAEXV?$basic_string@DU?$char_traits@D@s
td@@V?$allocator@D@2@@std@@@Z)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall RomanType::RomanType(void)" (??0RomanType@@QAE@XZ)
Debug/main.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

main.exe - 3 error(s), 0 warning(s)

here is the source code:

main.cpp



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

int main()
{
RomanType roman1;
string romannum = "2";

roman1.SetRoman(romannum);

return 0;

}



RomanType.cpp


#include "RomanType.h"
#include <string>
RomanType::RomanType()
{
roman="";
}

void RomanType::SetRoman(romannum)
{
roman = romannum;
}




RomanType.h


#ifndef ROMANTYPE_H
#define ROMANTYPE_H
class RomanType
{
public:
RomanType::RomanType();
void RomanType::SetRoman(string romannum);

private:
string roman;

};
#endif


thanks for any help

vicodin451
November 30th, 2003, 10:42 PM
The following compiles and links just fine for me...

main.cpp:

#include &lt;iostream&gt;
#include &lt;string&gt;
#include "RomanType.h"

using namespace std;

int main()
{
RomanType roman1;
string romannum = "2";

roman1.SetRoman(romannum);

return 0;

}


RomanType.cpp:

#include "RomanType.h"
#include &lt;string&gt;
RomanType::RomanType()
{
roman="";
}

void RomanType::SetRoman( std::string romannum)
{
roman = romannum;
}


RomanType.h:

#ifndef ROMANTYPE_H
#define ROMANTYPE_H
#include &lt;string&gt;

class RomanType
{
public:
RomanType();
void SetRoman(std::string romannum);

private:
std::string roman;

};
#endif

parsley
November 30th, 2003, 11:07 PM
hmm. well i'm using microsoft visual c++ 6, and i open the main.cpp file then click the compile icon, and it compiles fine. then i click the execute program, and it gives me the above error. i executing with just the main.cpp, and also with all three files open, and it gives me the same error. any other suggestions, thanks in advance.

vicodin451
November 30th, 2003, 11:21 PM
Originally posted by parsley
hmm. well i'm using microsoft visual c++ 6, and i open the main.cpp file then click the compile icon, and it compiles fine. then i click the execute program, and it gives me the above error. i executing with just the main.cpp, and also with all three files open, and it gives me the same error. any other suggestions, thanks in advance.

You say it compiles fine, and you mention linker errors, but you say you are able to execute the program? Did you notice that I changed your code a bit? I compiled & linked it, but I did not run it...

Can you clarify what is happening?

parsley
November 30th, 2003, 11:30 PM
i compile the main.cpp file without any errors. then i tried to execute the program and got the linker error. the program never successfully executed. thanks again for the help.

vicodin451
December 1st, 2003, 07:39 AM
Originally posted by parsley
i compile the main.cpp file without any errors. then i tried to execute the program and got the linker error. the program never successfully executed. thanks again for the help.

ZIP up your project and post it. Be sure to remove the DEBUG and RELEASE folders, and the .NCB / .OPT files. Also, if the project includes resources, be sure to include the .RES folder.

parsley
December 1st, 2003, 08:40 AM
ok, here (http://s88765076.onlinehome.us/RomanType.zip) is the zip (you may have to rightclick to save it)

vicodin451
December 1st, 2003, 08:46 AM
Originally posted by parsley
ok, here (http://s88765076.onlinehome.us/RomanType.zip) is the zip (you may have to rightclick to save it)

:)
You need to add RomanType.CPP and RomanType.H to your project. Project | Add to Project | Files... and choose RomanType.CPP and RomanType.H.

parsley
December 1st, 2003, 09:41 AM
thanks so much!! man i feel stupid :)...it compiles and executes fine now. however, i ran into another issue...when i try to use a cout statement in my RomanType.cpp, i get an undeclared identifier error. i included iostream in every one of my files, but it didn't help. here (http://s88765076.onlinehome.us/RomanType2.zip) is the zipped project. this is the RomanType.cpp file:
<blockquote><pre>
#include "RomanType.h"
#include &lt;string&gt;
#include &lt;iostream&gt;

RomanType::RomanType()
{
roman="";
}


void RomanType::SetRoman(std::string romannum)
{
roman = romannum;

cout&lt;&lt;"In SetRoman"&lt;&lt;endl;
}
</pre></blockquote>

vicodin451
December 1st, 2003, 09:44 AM
Originally posted by parsley
thanks so much!! man i feel stupid :)...it compiles and executes fine now. however, i ran into another issue...when i try to use a cout statement in my RomanType.cpp, i get an undeclared identifier error. i included iostream in every one of my files, but it didn't help. here (http://s88765076.onlinehome.us/RomanType2.zip) is the zipped project. this is the RomanType.cpp file:
<blockquote><pre>
#include "RomanType.h"
#include &lt;string&gt;
#include &lt;iostream&gt;

RomanType::RomanType()
{
roman="";
}


void RomanType::SetRoman(std::string romannum)
{
roman = romannum;

cout&lt;&lt;"In SetRoman"&lt;&lt;endl;
}
</pre></blockquote>


use std::cout and std::endl.
You could alternatively add "using namespace std;" near the top of the file.

parsley
December 1st, 2003, 12:27 PM
thanks so much for all the help. i've got all the functions to compile and run, now i just have to get the roman numeral logic. thanks again :)