0
#include <iostream>
using namespace std;

class Country;
{
private:
  char Name;
  double Rank;

public:
  void getN(char N, int RN);
  void EnterName();
  void EnterRN();
};

void Country::getN(char N, double RN)
{
  Name = N;
  Rank = RN;
}

void Country::EnterName();
{    
  cout << " The name of the country is " << N << endl;
}

void CSE::EnterRN();
{
  cout << " The rank of the country is " << RN << endl;
}

int main()
{
  void CSE::Name1;
  Name1.EnterName();
  Name1.EnterRN();
  Name1.getN('Country', 25)

  return 0;
}

Suppose I am making a code that takes Country and Rank from the user itself. I want to make it accessible by all. I am getting a lot of errors! Help me please. I am just a beginner.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • you can not declare any void variable or member unless it is a function which returns nothing! – newbie Feb 17 '21 at 06:32
  • And the compiler is right, I don't see any declaration of `CSE::Name1` either, do you? Also, what is `void CSE::Name1;` supposed to do? Declare a local variable? – Lukas-T Feb 17 '21 at 06:32
  • You must remove the semicolons after the function definitions: of `void Country::EnterName();` and `void CSE::EnterRN();`. Should the latter be `void Country::EnterRN();`? Inside these functions you print `N` and `RN` which don't exist either. Did you mean `Name` and `Rank`? This code is full of typos. – Lukas-T Feb 17 '21 at 06:38

3 Answers3

0

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 (' ').

D-RAJ
  • 3,263
  • 2
  • 6
  • 24
0

You have got plenty of errors:

  1. In this line:

    class Country;
    

    You are ending the syntax in the same line. You're not making any classes. Exactly with void Country::EnterName(); and void Country::EnterRN();.

  2. After fixing the first problem, we get another. Note this line:

    void getN(char N, int RN);
    

    and its definition

    void Country::getN(char N, double RN) 
    

    The declaration and its definition prototypes must match.

  3. You need to use the type of std::string or const char * to store the string constant "Country". A single char guarantees to store a single ASCII character (1-byte length).

  4. In the EnterName() and EnterRN()'s definitions, you are confused with function arguments and class attributes. The scope of the function arguments such as:

    getN(char N, int RN)
    

    is only visible to the function, nowhere outside. Instead, you need to use Name and Rank.

  5. You forgot to introduce a semicolon before the main function exit:

    Name1.getN("Country", 25)
    
  6. Lastly, note that the object must be initialized before it's used. You attempted to print the results before you called getN(). Just shift it to the top:

    Country Name1;
    Name1.getN("Country", 25);
    Name1.EnterName();
    Name1.EnterRN();
    
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
0

Your code is full of typos and logical errors, which happens and is o.k. to a degree when you are a beginner. So first some general hints:

  • Write a single function or declaration at a time, then compile to see if it works or not. Don't proceed until you know the current code works as intended. This way you can spot typos before they begin to pile up.
  • Turn your compiler warnings to maximum and learn to read your compilers output. It will tell you exactly what is wrong and where the error happened. Most of the time at least ;)
  • Never ignore warnings. Understand why the compiler issues a warning and fix it.
  • Naming is important in programming. In your code most functions do something completely different than the name would suggest and you confused yourself with that.

Now, let's get started:

  1. using namespace std; is considered bad practice. Not really an error, but something that's worth thinking about.

  2. There's a semicolon after

    class Country; // <---
    { ... }
    

    remove it. You would only need it in a forward declaration, but not when you provide a class body. The overuse of semicolons is general trend in your code.

  3. Country::Name; should probably be more than a single character? In C++ you use std::string for this.

  4. The definition of

    void Country::getN(char N, double RN) 
    

    does not match it's declaration

    void getN(char N, int RN);
    

    (different type for second parameter, you decide if the rank should be a double or an int). Anyway, from the usage of this function it's apparent that

    1. It should be named SetN, because it sets things and doesn't get anything. (Maybe something like SetNameAndRank would be even better because it's more descriptive of what the function actually does. (Or the implementation is wrong and it should really get the Name, it's not clear)
    2. The first parameter should be the std::string, because a name is not just a single character.
  5. There's a semicolon in the definition of

    void Country::EnterName();
    

    remove it, as you did in void Country::getN(char N, double RN)

  6. In the same function void Country::EnterName(); you attempt to print N. This is the name of a parameter passed to getN. You mean the member variable Name.

  7. Same problem for

    void CSE::EnterRN();
    

    Should this maybe be void Counter::EnterRN();? There is no class CSE.

  8. In the same function void Country::EnterRN(); you attempt to print RN. This is the name of a parameter passed to getN. You mean the member variable Rank.

  9. EnterRN and EnterName should be called PrintRN (Or better, because more descriptive printRank) and PrintName, because they print things and don't give the user the opportunity to enter something. (Take a look at main to see how you confused yourself with this naming)

  10. Now we get to the main-function:

    void CSE::Name1;
    

    doesn't make any sense. I think you wanted a local variable of type Country called Name1? That would be

    Country Name1;
    
  11. Typo, there's a missing semicolon after the function call

    Name1.getN('Country', 25)
    
  12. Also 'Country' is multi-character-literal. It has implementation defined behaviour and you want a string literal with " instead of ':

    //         V       V     V
    Name1.getN("Country", 25);
    
  13. Next you see the results of confusing naming. Your Enter-Funtions, don't enter anything but print things. If you rename the functions as proposed above you see that this is the wrong order:

    Name1.PrintName();
    Name1.PringRank();
    Name1.SetNameAndRang('Country', 25)
    

    When a function is named after what it does it should be easy to figure out the correct order of operations.

  14. Not an error, but the return 0; in main is not needed.

Here's the complete corrected code. I hope you can learn something from it and further your understanding of the language.

#include <iostream> // 1

class Country // 2
{
private:
  std::string Name; // 3
  double Rank;

public:
  void SetNameAndRank(std::string N, double RN); // 4, 4.1, 4.2
  void PrintName();
  void PrintRank();
};

void Country::SetNameAndRank(std::string N, double RN) // 4
{
  Name = N;
  Rank = RN;
}

void Country::PrintName() // 5, 6, 9
{
  std::cout << " The name of the country is " << Name << std::endl;
}

void Country::PrintRank() // 7, 8, 9
{
  std::cout << " The rank of the country is " << Rank << std::endl;
}

int main()
{
  Country Name1; // 10
  Name1.SetNameAndRank("Country", 25); // 11, 12, 13
  Name1.PrintName(); // 13
  Name1.PrintRank(); // 13
  
  // 14
}
Lukas-T
  • 11,133
  • 3
  • 20
  • 30