Thread: Class error

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    47

    Exclamation Class error

    Hello friends,
    I am trying to access Birthday object from peoples class and display it in main but why the program is getting compilation error?

    here is my code
    main.cpp
    Code:
    #include<iostream>#include "Birthday.h"
    #include "Birthday.cpp"
    #include "People.h"
    #include "People.cpp"
    using namespace std;
    
    
    
    
    int main(){
     Birthday birthObj(11,5,1992);
     People JasonSmith("Jason",birthObj);
     JasonSmith.printInfo();
    
    
    
    
    return 0;
    }
    Birthday.h

    Code:
    #ifndef BIRTHDAY_H#define BIRTHDAY_H
    
    
    
    
    class Birthday
    {
        public:
            Birthday(int m,int d,int y);
            void printDate();
    
    
        private:
            int month;
            int day;
            int year;
    };
    
    
    #endif // BIRTHDAY_H
    Birthday.cpp

    Code:
    #include "Birthday.h"#include<iostream>
    using namespace std;
    Birthday::Birthday(int m,int d,int y)
    {
      month = m;
      day = d;
      year= y;
    }
    
    
    void Birthday::printDate(){
    
    
      cout<<month<<"/ "<<day<<"/ "<<year<<endl;
    }
    People.h
    Code:
    #ifndef PEOPLE_H#define PEOPLE_H
    
    
    #include<string>
    #include "Birthday.h"
    #include "Birthday.cpp"
    using namespace std;
    class People
    {
        public:
            People(string x,Birthday bo);
            void printInfo();
    
    
        private:
            string name;
            Birthday dateOfBirth;
    };
    
    
    #endif // PEOPLE_H
    People.cpp

    Code:
    #include "People.h"#include "Birthday.h"
    #include<iostream>
    using namespace std;
    
    
    People::People(string x,Birthday bo)
    : name(x),dateOfBirth(bo)
    {
    
    
    }
    
    
    void People::printInfo(){
     cout<<name<<" was born on ";
     dateOfBirth.printDate();
    
    
    }
    Please be easy on me as i am still learning c++

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but why the program is getting compilation error?
    What exactly is this compile error?

    Also #including a .cpp file is usually a mistake. You should add the .cpp files to your project to be properly compiled, not #included.

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    47
    error is in Birthday.cpp

    Class error-error-jpg

    without including .cpp files the program is not even running no errors either

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    As I said in my previous post this error is being caused by #including that source file. You need be using a project and then add the .cpp files to your project for compiling.

    without including .cpp files the program is not even running no errors either
    Did you add the .cpp files to the project and re-build the sources?

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    47
    you mean i have to make a new project defining new paths

    from here

    Class error-project-jpg

    and then compile ?

  6. #6
    Registered User
    Join Date
    May 2019
    Posts
    47
    do note since i am creating mutipleclass files for each topic i learn i am making seperate folders fore each and then creating class inside that folder

    in this case its composition folder

    in my last tutorial where i learnt member initializer
    i made separate folder for that and made seperate main,header and .cpp file and it compiled just fine

    code of member initializer

    Code:
    #include<iostream>#include "Sally.h"
    #include "Sally.cpp"
    using namespace std;
    
    
    
    
    int main(){
    
    
    Sally so(45,76);
    so.print();
    
    
    return 0;
    }
    header file
    Code:
    #ifndef SALLY_H#define SALLY_H
    
    
    
    
    class Sally
    {
        public:
            Sally(int a,int b);
            void print();
        protected:
    
    
        private:
            int regVar;
            const int constVar;
    };
    
    
    #endif // SALLY_H
    sally.cpp

    Code:
    #include "Sally.h"#include<iostream>
    
    
    using namespace std;
    
    
    
    
    Sally::Sally(int a,int b)
    //member intializer is needed to call constant variables
    : regVar(a),
    constVar(b)
    {
    
    
    }
    
    
    void Sally::print(){
     cout<<"regular variable is "<<regVar<<endl;
     cout<<"constant variable is "<<constVar<<endl;
    }
    Last edited by sash_007; 09-20-2019 at 05:23 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You have multiple #include "somefile.cpp" lines.

    #include<iostream>
    #include "Sally.h"
    #include "Sally.cpp" //!!<< This is wrong!

    You add the .h and .cpp files to your project in the IDE, then it will do the right thing when it comes to compiling.
    Class error-cb_add_files-png

    So you end up with Birthday.cpp, People.cpp, main.cpp (and any others) shown in the sources "folder" in the IDE.
    And likewise for any header files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2019
    Posts
    47
    ok thanks i will try this
    Last edited by sash_007; 09-21-2019 at 05:35 PM.

  9. #9
    Registered User
    Join Date
    May 2019
    Posts
    47
    ok i made new project watermelon

    and added all source and header files but still getting error?

    Class error-still_error-jpg

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Okay, where is your code for main?

    Please cut and paste the code into a post (inside code tags), pictures tend to be harder to read.

    It would also help if you cut and pasted the error messages, your picture is cutting a lot of the error messages off.

  11. #11
    Registered User
    Join Date
    May 2019
    Posts
    47
    main.cpp

    Code:
    #include<iostream>#include "Birthday.h"
    
    
    #include "People.h"
    
    
    using namespace std;
    
    
    
    
    int main(){
     Birthday birthObj(11,5,1992);
     People JasonSmith("Jason",birthObj);
     JasonSmith.printInfo();
    
    
    
    
    return 0;
    }
    birthday.cpp
    Code:
    #include "Birthday.h"#include<iostream>
    using namespace std;
    Birthday::Birthday(int m,int d,int y)
    {
      month = m;
      day = d;
      year= y;
    }
    
    
    void Birthday::printDate(){
    
    
      cout<<month<<"/ "<<day<<"/ "<<year<<endl;
    }
    birthday.h

    Code:
    #ifndef BIRTHDAY_H#define BIRTHDAY_H
    
    
    
    
    class Birthday
    {
        public:
            Birthday(int m,int d,int y);
            void printDate();
    
    
    
    
    
    
        private:
            int month;
            int day;
            int year;
    };
    
    
    #endif // BIRTHDAY_H
    people.cpp

    Code:
    #include "People.h"#include "Birthday.h"
    #include<iostream>
    using namespace std;
    
    
    People::People(string x,Birthday bo)
    : name(x),dateOfBirth(bo)
    {
    
    
    }
    
    
    void People::printInfo(){
     cout<<name<<" was born on ";
     dateOfBirth.printDate();
    
    
    }
    people.h
    Code:
    #ifndef PEOPLE_H#define PEOPLE_H
    
    
    #include<string>
    #include "Birthday.h"
    #include "Birthday.cpp"
    using namespace std;
    class People
    {
        public:
            People(string x,Birthday bo);
            void printInfo();
    
    
        private:
            string name;
            Birthday dateOfBirth;
    };
    
    
    #endif // PEOPLE_H
    multiple definition error

    Class error-error_multiple_definition-jpg

  12. #12
    Registered User
    Join Date
    May 2019
    Posts
    47
    ok things worked when i removed

    #include "Birthday.cpp"
    from people.h

    new people.h
    Code:
     #ifndef PEOPLE_H#define PEOPLE_H
    
    
    #include<string>
    #include "Birthday.h"
    
    
    using namespace std;
    class People
    {
        public:
            People(string x,Birthday bo);
            void printInfo();
    
    
        private:
            string name;
            Birthday dateOfBirth;
    };
    
    
    #endif // PEOPLE_H

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please cut and paste your error messages into a post. That picture is not showing everything.

  14. #14
    Registered User
    Join Date
    May 2019
    Posts
    47
    Quote Originally Posted by jimblumberg View Post
    Please cut and paste your error messages into a post. That picture is not showing everything.
    i got this working (check my last reply)thank you and Salem for helping me learning how to add files in project and not to include .cpp files like i did before.. much appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ class error
    By ssm in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2016, 02:32 AM
  2. Replies: 7
    Last Post: 11-10-2007, 05:17 AM
  3. class error?think?
    By Hitachi in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2005, 09:26 AM
  4. Template <class T1, class T2, class T3> error LNK2019
    By JonAntoine in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2004, 12:25 PM
  5. Class Error
    By LloydUzari in forum C++ Programming
    Replies: 4
    Last Post: 08-08-2004, 09:58 AM

Tags for this Thread