Trouble with using strings in classes

Never mind... I found my mistake. I forgot to put romanType:: in front of the method name.

I'm getting an error that says my variable "romanNum" is undefined in my romanImp.cpp file. Any idea what I'm doing wrong here? I've read a few posts here about strings in classes, but none of the solutions seem to change things. I'm using Visual C++ Express 2010.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// romanImp.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include "roman.h"

using namespace std;

void romanType::setRoman(string num)
{
	romanNum = num;
}

romanType::romanType()
{
	arab = 1;
}

romanType::romanType(int num)
{
	arab = num; 
}


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

using namespace std;

class romanType
{
public:
	void setRoman(string num);
	romanType();
	romanType(int num);
private:
	int arab;
	string romanNum;
};


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

#include <iostream>
#include <iomanip>
#include <string>
#include "roman.h" 

using namespace std;

int main()
{
	romanType myNumber(56);
}


Thanks for any advice.
Last edited on
You just forgot the classname in the function definintion

1
2
3
4
void romanType::setRoman(string num)
{
	romanNum = num;
}


Without the classname you defined being added (as in your original code) romanNum would be looked for by the compiler as a local or global variable, and since you defined it in your class, it's not found. That's why you got the undefined error. Adding the classname means your function is part of your class and allows the access to the variables you defined in the class declaration.

Might be a good idea to add void to the other functions too.
Last edited on
Thanks Moooce.
Topic archived. No new replies allowed.