Hello All,
I am very new to C++ and have been working through some of the class method projects. I am having a issue with one that I cannot seem to figure out what is incorrect with the code.
I have played around with it using examples from various books/web resources and have dug pretty deep into web forums and support groups. But I still cannot seem to find an answer. Its acting like a "out of scope" problem but I do not see how. Any help would be appreciated. Thx.....
// START INCLUDE SECTION
#include <iostream>
// END INCLUDE SECTION
// START CLASS DEFINITION SECTION
class Ship
{
private:
char shipName;
public:
void SetshipName(char sName);
char GetshipName(void);
};
void Ship::SetshipName(char sName)
{
shipName = sName;
}
char Ship::GetshipName(void)
{
return shipName;
}
// END CLASS DEFINITION
// START INT MAIN SECTION
int main()
{
Ship cruiser;
cruiser.SetshipName(master); // THIS IS THE FLAGGED LINE
std::cout << " The name of your ship is ";
std::cout << cruiser.GetshipName() << " !";
return 0;
}
// END INT MAIN SECTION
There are two main problems.
The error the compiler is reporting is simple to fix, you just need to change line 35 to
cruiser.SetshipName("master"); //Enclose name in quotes
Without the quotes master is a varaible (which you have not declared).
The second is that you have used char as the type for shipName, etc. This is a single character, eg 'd', when you probably want an array of charcters, or a string as the type.
See http://www.cplusplus.com/doc/tutorial/variables.html which discusses char arrays and strings for more info.
Thx for the reply Faldrax.
I thought I tried that before but maybe it was when I was playing with some other code. Thanks for explaining it the way you did. All the examples really did not touch on that. Although I am sure its mentioned somewhere else I did not seem to find it for the searches I was doing :).
Also, I am going to work on using std::string instead of the char arrays I think. From what it sounds like that would be the better way to learn due to limitation of char arrays.