Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

error: 'Roll' was not declared in this scope

+2 votes
asked Feb 17 by Adarsh Dhital (140 points)

#include <iostream>

using namespace std;

class Section;

{

private:

    char Name;

    double Roll;

public:

    getN(char N, double RN);

    {

        Name = N;

        Roll = RN;

    }

};

void EnterName()

{

    cout << "Enter the name of the student" << Name;

}

void EnterRN()

{

    cout

        << "enter the rn" << RN;

};

int main()

{

    char Section::nnn;

    nnn.getN(Name, Roll);

    cout << "enter the name" << nnn.EnterName << endl;

    cout << "enter the roll" << nnn.EnterRN << endl;

    return 0;

}

1 Answer

0 votes
answered Feb 18 by Peter Minarik (19,180 points)

Mistakes

Semicolons

You often put semicolon after class or function definition, while in the next line you are providing the body of the class or the function. In those cases you mustn't add a semicolon.

E.g.:

  • class Section;
  • getN(char N, double RN);

Class Initialisation

You should initialise your class with a constructor. The constructor has the same name as the class and it can receive parameters to set up initial values. It doesn't have a return value (as it is always the class itself)

E.g.:

Section(char * name, double roll) :
    Name(name),
    Roll(roll)
{
}

Member Functions

Functions that belong to a specific class must be declared within that class. Your functions EnterName and EnterRN are declared (and defined) outside of the scope of your Selection class.

Data Types

To store a "text", you have to use a string. You can either use a C language type string (C-String), which is a character array: char *; or you can use a C++ string: std::string. I'd recommend using the latter since you're writing C++ code, so why not use the language features? The type char is meant to store a single character only not a sequence of them (text).

A Better Solution

I rewrote the whole code as it had quite a numerous problems (both syntax and both design problems).

Note: I'm not entirely sure what your class and your code wants to do, so feel free to change it. :)

#include <iostream>
#include <string>

class Section
{
private:
    std::string _name;
    double _roll;

public:
    // Constructor to initialise the class
    Section(std::string name, double roll) :
        _name(name),
        _roll(roll)
    {
    }
    
    const std::string & GetName() const // the const means this function would not change the class, remove const if you want your function to change the state of the class
    {
        return _name;
    }
    
    double GetRoll() const // the const means this function would not change the class, remove const if you want your function to change the state of the class
    {
        return _roll;
    }
};

int main()
{
    std::string name;
    double roll;
    std::cout << "Enter the name of the student: ";
    std::cin >> name;
    std::cout << "Enter the roll: ";
    std::cin >> roll;
    
    Section section(name, roll);
    std::cout << "Section: " << section.GetName() << ", " << section.GetRoll() << std::endl;
    
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...