When defining a class, you cannot do it like this: class Country;
. This is a declaration. To properly define a class, you need to give it a body,
class Country { ... }
Semicolon tells the compiler that the statement is over from there. For classes, it separates the class from its body, thus will give you a undefined reference
linker error.
Its the same for function definitions too. So when your defining a function, do not separate the function body and the function signature using a semicolon.
When defining member functions, the two function signatures (return type, function name, function parameters) must match. In this,
void Country::getN(std::string N, double RN)
{
Name = N;
Rank = RN;
}
The class Country
does not have a function void Country::getN(std::string N, double RN)
, but it does have a function void getN(std::string N, int RN)
. Im guessing the member function definition is correct and if so, the declaration should match with it.
class Country
{
...
void getN(std::string N, double RN);
...
};
You cannot instantiate an object in C++ using this syntax: void CSE::
. To properly instantiate the class you need to do it like follow,
CSE Name1;
But you don't have a class named CSE
. You can only instantiate classes that are available or defined. So my guessing is your looking for,
Country Name1;
char
holds information about a single ASCII character. It cannot store information about multiple characters (string). To store a string, you either have to use a C style char buffer,
char Name[10]; // 10 is the maximum name length
Or use std::string
for which the string size can grow.
#include <string>
...
std::string Name;
You class doesn't have member variables called `N` and `RN`. What you do have is two of them defined as function parameters in the function `Country::getN()`. You cannot use another function's parameters in another different function. I think what your looking for by `N` and `RN` is `Name` and `Rank`.
void Country::EnterName()
{
cout << " The name of the country is " << Name << endl;
}
void Country::EnterRN()
{
cout << " The rank of the country is " << Rank << endl;
}
After putting everything together, we will have a our final code,
#include <iostream>
#include <string> // For std::string
using namespace std;
class Country
{
private:
std::string Name;
double Rank;
public:
void getN(std::string N, double RN);
void EnterName();
void EnterRN();
};
void Country::getN(std::string N, double RN)
{
Name = N;
Rank = RN;
}
void Country::EnterName()
{
cout << " The name of the country is " << Name << endl;
}
void Country::EnterRN()
{
cout << " The rank of the country is " << Rank << endl;
}
int main()
{
Country Name1;
Name1.EnterName();
Name1.EnterRN();
Name1.getN("Country", 25); // Statements are terminated by ';'. Don't forget it!
return 0;
}
Note: In C++, strings use double quotes (" "
) and characters (char
) use single quotes (' '
).