12 Replies - 777 Views - Last Post: 15 March 2020 - 01:41 AM Rate Topic: -----

#1 1337 errors   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 20

Error draclaration of and not declared in this scope

Posted 14 March 2020 - 08:45 AM

Hi, i'm a beginner in C++ and i just finished the OOP chaptop of an online course. I started a mini-project about facultacy admission.
But when i tought i finished my code, i got some new errors that i can't figure out what they mean.
Also, i got an error (see below) and i would appreciate if someone could tell when and why this error occurs?
In member function 'void Students::display() const':
D:\...\Students.cpp:39:42: error: expected primary-expression before ')' token
         for(const auto &Student: Students) 


Anyway, my errors are now in main.
The code of my main is:
#include <iostream>
#include "Students.h"

bool Students::add_student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac,
                           double grade1_ex1, double grade2_ex1, double grade3_ex1, double grade1_ex2,
                           double grade2_ex2, double grade3_ex2);

bool Students::add_student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac,
                           double grade1_ex1, double grade2_ex1, double grade3_ex1, double grade1_ex2,
                           double grade2_ex2, double grade3_ex2)
{
    if(vect.add_student(name, birth_day, birth_month, birth_year, grade_bac, grade1_ex1, grade2_ex1, grade3_ex1,
            grade1_ex2, grade2_ex2, grade3_ex2)){
        std::cout << name << " was added!" << std::endl;
    } else {
        std::cout << name << " already exists!" << std::endl;
    }

}
int main() {
    Students my_stud;
    my_stud.display();

    add_student("mark", 20, 04, 1998, 9.50, 8.75, 8.95, 9.00, 7.50, 6.30, 7.00);
    return 0;



And the errors are:
error: declaration of 'bool Students::add_student(std::__cxx11::string, int, int, int, double, double, double, double, double, double, double)' outside of class is not definition [-fpermissive]
                            double grade2_ex2, double grade3_ex2); 


In member function 'bool Students::add_student(std::__cxx11::string, int, int, int, double, double, double, double, double, double, double)':
D:\...\main.cpp:12:13: error: 'class std::vector<Student>' has no member named 'add_student'
     if(vect.add_student(name, birth_day, birth_month, birth_year, grade_bac, grade1_ex1, grade2_ex1, grade3_ex1, 


In function 'int main()':
D:\...\main.cpp:24:79: error: 'add_student' was not declared in this scope
     add_student("mark", 20, 04, 1998, 9.50, 8.75, 8.95, 9.00, 7.50, 6.30, 7.00);


My mini-project is done in the following way: main.c , Student.h, Student.cpp, Students.h, Students.cpp and it would be too much code to copy/paste to run it...I think is better to download it and run(to see the errors better) from here https://drive.google...nULZKMqAo4AK0wo

Is This A Good Question/Topic? 0
  • +

Replies To: Error draclaration of and not declared in this scope

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15568
  • View blog
  • Posts: 62,341
  • Joined: 12-June 08

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 08:47 AM

Function definitions should be in the .H.

The full body of the functions in the .CPP.

Both of these should NOT be in the same place your MAIN is.
Was This Post Helpful? 0
  • +
  • -

#3 1337 errors   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 20

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 08:51 AM

I have declared functions prototypes in .h files and the body of functions are in .h files, but i tought that i must rewrite that add_student function in main too.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15568
  • View blog
  • Posts: 62,341
  • Joined: 12-June 08

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 08:52 AM

It doesn't really make sense to have it all in two places.

Again, if you are using .H/.CPP pitch the code next to the main.
Was This Post Helpful? 0
  • +
  • -

#5 1337 errors   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 20

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 09:02 AM

I put all of my code into main.cpp and i get only one error, but i can't understand why.

The error is for this line(that is into int main)
add_student("mark", 20, 04, 1998, 9.50, 8.75, 8.95, 9.00, 7.50, 6.30, 7.00)

and the error is saying
In function 'int main()':
D:\Exercitii CURS C++\stressedOut\main.cpp:234:79: error: 'add_student' was not declared in this scope
     add_student("mark", 20, 04, 1998, 9.50, 8.75, 8.95, 9.00, 7.50, 6.30, 7.00);


My full code:
#include <iostream>
#include <vector>
#include <string>

class Student {
private:
    std::string name;
    int birth_day;
    int birth_month;
    int birth_year;
    double grade_bac;
    double grade1_ex1;
    double grade2_ex1;
    double grade3_ex1;
    double grade1_ex2;
    double grade2_ex2;
    double grade3_ex2;
    double average;

public:
    Student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac, double grade1_ex1, double grade2_ex1,
            double grade3_ex1, double grade1_ex2, double grade2_ex2, double grade3_ex2);
    Student(const Student &source);
    ~Student();

    void set_name(std::string name)
    {
        this->name = name;
    }

    std::string get_name() const
    {
        return name;
    }

    void set_birth_day(int birth_day)
    {
        this->birth_day = birth_day;
    }

    int get_birth_day() const
    {
        return birth_day;
    }

    void set_birth_month(int birth_month)
    {
        this->birth_month = birth_month;
    }

    int get_birth_month() const
    {
        return birth_month;
    }

    void set_birth_year(int birth_year)
    {
        this->birth_year = birth_year;
    }

    int get_birth_year() const
    {
        return birth_year;
    }

    void set_grade_bac(double grade_bac)
    {
        this->grade_bac = grade_bac;
    }

    double get_grade_bac() const
    {
        return grade_bac;
    }

    void set_grade1_ex1(double grade1_ex1)
    {
        this->grade1_ex1 = grade1_ex1;
    }

    double get_grade1_ex1() const
    {
        return grade1_ex1;
    }

    void set_grade2_ex1(double grade2_ex1)
    {
        this->grade2_ex1 = grade2_ex1;
    }

    double get_grade2_ex1() const
    {
        return grade2_ex1;
    }

    void set_grade3_ex1(double grade3_ex1)
    {
        this->grade3_ex1 = grade3_ex1;
    }

    double get_grade3_ex1() const
    {
        return grade3_ex1;
    }

    void set_grade1_ex2(double grade1_ex2)
    {
        this->grade1_ex2 = grade1_ex2;
    }

    double get_grade1_ex2() const
    {
        return grade1_ex2;
    }

    void set_grade2_ex2(double grade2_ex2)
    {
        this->grade2_ex2 = grade2_ex2;
    }

    double get_grade2_ex2() const
    {
        return grade2_ex2;
    }

    void set_grade3_ex2(double grade3_ex2)
    {
        this->grade3_ex2 = grade3_ex2;
    }

    double get_grade3_ex2() const
    {
        return grade3_ex2;
    }

    double set_average()
    {
        double sum_ex1 = grade1_ex1 + grade2_ex1 + grade3_ex1;
        double sum_ex2 = grade1_ex2 + grade2_ex2 + grade3_ex2;
        double avg1 = sum_ex1/3.0;
        double avg2 = sum_ex2/3.0;

        double average = 0.25*grade_bac + ((avg1 + avg2)/2)*0.75;
        return average;
    }


    void display();
};

//Students class
class Students {
private:
    std::vector<Student> vect;

public:
    Students();
    ~Students();

    bool add_student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac, double grade1_ex1, double grade2_ex1,
                     double grade3_ex1, double grade1_ex2, double grade2_ex2, double grade3_ex2);

    void display();
};

//Functions for Student class
Student::Student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac, double grade1_ex1,
                 double grade2_ex1, double grade3_ex1, double grade1_ex2, double grade2_ex2, double grade3_ex2)
        :name{name}, birth_day{birth_day}, birth_month{birth_month}, birth_year{birth_year}, grade_bac{grade_bac},
         grade1_ex1{grade1_ex1}, grade2_ex1{grade2_ex1}, grade3_ex1{grade3_ex1}, grade1_ex2{grade1_ex2},grade3_ex2{grade3_ex2}{

}

Student::Student(const Student &source)
        :Student{source.name, source.birth_day, source.birth_month, source.birth_year, source.grade_bac, source.grade1_ex1,
                 source.grade2_ex1, source.grade3_ex1, source.grade1_ex2, source.grade2_ex2, source.grade3_ex2}{

}

Student::~Student() {}

void Student::display()
{
    std::cout << "==============================Informations===========================" << std::endl;
    std::cout << "Name: " << name << std::endl;
    std::cout << "Birth date(dd/mm/yy): " << birth_day << "/" << birth_month << "/" << birth_year << std::endl;
    std::cout << "Baccalaureate grade: " << grade_bac << std::endl;
    std::cout << "First exam grades: " << grade1_ex1 << ", " << grade2_ex1 << " and " << grade3_ex1 << std::endl;
    std::cout << "Second exam grades: " << grade1_ex2 << ", " << grade2_ex2 << " and " << grade3_ex2 << std::endl;
    std::cout << "Average admission grade: " << set_average() << std::endl;
    std::cout << "==============================Informations===========================" << std::endl;
}

//Students functions
Students::Students(){

}

Students::~Students() {

}

bool Students::add_student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac,
                           double grade1_ex1, double grade2_ex1, double grade3_ex1, double grade1_ex2, double grade2_ex2, double grade3_ex2)
{
    for(const Student &student: vect){
        if(student.get_name() == name){
            return false;
        }
    }
    // If you get here then the student wasn't found. Insert it.
    Student temp (name, birth_day, birth_month, birth_year,
                  grade_bac, grade1_ex1, grade2_ex1,
                  grade3_ex1, grade1_ex2, grade2_ex2, grade3_ex2);
    vect.push_back(temp);
    return true;
}

void Students::display()
{
    if(vect.size() == 0){
        std::cout << "Error: There are no students!\n" << "Please add one student or more!" << std::endl;
    } else {
        std::cout << "====================================================" << std::endl;
        for(auto &student: vect)
            student.display();
        std::cout << "===================================" << std::endl;
    }
}
int main() {
    Students my_stud;
    my_stud.display();

    add_student("mark", 20, 04, 1998, 9.50, 8.75, 8.95, 9.00, 7.50, 6.30, 7.00);
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 09:09 AM

Also posted here.

Silly question, have you tried to get the program working for a single student?

Silly observation: Having one class named Student and another named "Students" is going to get confusing really quickly.

By the way I really don't see any need for that silly Students class, just create yourself a vector<Student> in main() and populate the vector with your students. C++ doesn't require everything to be inside some silly class.


Jim
Was This Post Helpful? 2
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15568
  • View blog
  • Posts: 62,341
  • Joined: 12-June 08

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 09:15 AM

That's all in one file?

@jim - the class is probably part of the homework to teach, well, classes. Maybe. Probably. Who knows.
Was This Post Helpful? 0
  • +
  • -

#8 1337 errors   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 20

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 09:18 AM

@jimblumberg I am really exhausted and a little bit mad because i am trying to solve this problem for more than 8 hours and i tried yesterday too but i can't. I just can't find why that add_student from the main says "Use of undeclared identifier 'add_stud'". This is why i posted my problem on another forum.

@modi123_1 Yes, i chosed to use 2 classes for this mini-project so i can learn classes better :)
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is online

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 09:25 AM

Quote

the class is probably part of the homework to teach, well, classes. Maybe. Probably. Who knows.

Maybe, but that still doesn't change my opinion of that silly Students class, all it is doing is trying to encapsulate a std::vector<Student>, the vector is already encapsulating his Student class.


Edit:

Quote

I just can't find why that add_student from the main says "Use of undeclared identifier 'add_stud'".

It is saying that because there is no global function named add_student anywhere in your code.

This part of the reason I am so against that silly Students class, it's just confusing you.

Jim

This post has been edited by jimblumberg: 14 March 2020 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7291
  • View blog
  • Posts: 24,675
  • Joined: 05-May 12

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 10:30 AM

1337 errors: Look closely at line 232 and 235 of your post #5. Can you explain why you wrote line 232 that way, and line 235 a different way?

I agree with jimblumberg: The Students class is pointless. It looks like some one who writes code in Java learning how to write code in C++.
Was This Post Helpful? 1
  • +
  • -

#11 1337 errors   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 20

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 11:04 AM

Skydiver I was really confused...but i should've written my_stud.add_stud();
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg   User is online

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: Error draclaration of and not declared in this scope

Posted 14 March 2020 - 02:32 PM

Here is a "slightly" simpler version of your program, notice the lack of all the setters and getters that aren't used, and the lack of the copy constructor and destructor which are also not necessary. It is also using more modern C++ features like uniform initialization, the default keyword to create a "default" no argument constructor.

Spoiler


Oh, and I almost forgot no silly Students class.


Jim
Was This Post Helpful? 3
  • +
  • -

#13 1337 errors   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 20

Re: Error draclaration of and not declared in this scope

Posted 15 March 2020 - 01:41 AM

jimblumberg Thank you for your code, it is much simpler and narrower than mine. I did my code that way because I was learned to do it that way. Now I understood what you were saying about that Students class and that vector to be created into main.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1