Set and Get in .cpp/.h not working

I'm writing a simple code that, with help from the constructor, sets a string variable and gets it. The only problem is, I haven't found anywhere that would help me accomplish this. Here's what I think would work:

1
2
3
4
5
6
7
8
9
10
11
12
 // main.cpp
#include <iostream>
#include "SolsticeClass.h"

int main()
{

SolsticeClass s("byteofserial");

    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 // SolsticeClass.h
#ifndef SOLSTICECLASS_H
#define SOLSTICECLASS_H


class SolsticeClass
{
public:
void setName(std::string sName);
    std::string getName();

    private:
std::string Name;

};
#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 // SolsticeClass.cpp
#include "SolsticeClass.h"
#include <iostream>

SolsticeClass::SolsticeClass(std::string cName){
setName(cName);
}

void setName(std::string sName)
{
    Name = sName;
}

std::string getName()
{
return Name;
}


Unfortunately, it freaks out with and without the object. I have no idea what to do.
Let us first look at your constructor definition:
1
2
3
SolsticeClass::SolsticeClass(std::string cName){
setName(cName);
}

Here you can see that you wrote out the full name of the function:
1
2
//Class name::member function name
SolsticeClass::SolsticeClass(...


Now let's look at your other functions:
1
2
3
4
//     function name
void setName(...
//                function name
std::string getName()...

Note that I do not call these "member" function names. These two functions do not belong to any class or struct. They are independent of class Solstice.
In addition, this is what the compiler expects:
1
2
3
4
//     Class name::member function name
void SolsticeClass::SetName(...
//              Class name::member function name
std::string SolsticeClass::getName()...

However, you did not provide the definitions of the "member" functions, causing a compiler error.
Class::Function() is a completely different function from Function() just as the variables are different in this code:
1
2
void funky(int a){}    //variable a
void funky2(int a){}  //different variable a 


In short, don't forget to include the name of the class and the scope operator(::).

Edit:
You should probably #include <string> in your SolsticeClass.cpp file. The iostream library is not necessary in the same file because you are not using anything(like cout or cin) in it.
Last edited on
In your definition of SolsticeClass there is no declaration of a constructor which takes a string as a parameter. You cannot define such a function outside of the class definition without declaring it therein.
Daleth,

I'm not quite sure why Class in Class::Function was Solstice, instead of SolsticeClass. May I ask you to elaborate further? Thank you :)
Sorry, I have a bad habit of missing details when reading. :\ I edited by post, but consider Cire's as well.
Topic archived. No new replies allowed.