Thread: C++ error C2011

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    4

    C++ error C2011

    Hi, Im working with making my first class and I have everything set up but when i try to build, i get the apparently "famous" c2011 error. I've tried different methods of solving this problem after searching online, but nothing worked.
    Here is my code
    This code creates the gradebook object
    Code:
    // 
    
    #include "stdafx.h"
    #include <iostream> 
    #include "gradebook.h" //this is the header file for my class
    
    using namespace std;
    
    
    
    int main()
    {
    	
    	Gradebook mygradebook ("CS 101") ; // 
    
    }
    This is my header file.
    Code:
    #include "stdafx.h"
    #include <iostream>
    
    #include <string>
    
    using namespace std;
    
    class Gradebook {
    public:
    	Gradebook (string name);
    	void Setname(string name);
    	string Getname();
    	void DisplayMessage( );
    private :
    	string Coursename ; };
    This is my .cpp file, for the header,
    Code:
    #include "stdafx.h"
    #include "gradebook.h"
    #include <string>
    
    
    
    
    
    	using namespace std;
    class Gradebook {
    public:
    	Gradebook::Gradebook (string name){  //constructor statement
    	Setname(name);
    	}
    	void Gradebook::Setname(string name) {   // Mutator method
    		Coursename = name;
    	}
    	string Gradebook::Getname() {
    	return Coursename ;
    	}
    	
    	void Gradebook::DisplayMessage( ){ //display message statement
    	cout << "Welcome To The Gradebook For " << Getname() << endl ; 
    	}
    
    private: 
    	string Coursename ;
    };
    The error i get is
    Code:
    gradebook.cpp(11) error C2011: 'Gradebook' : 'class' type redefinition
              gradebook.h(8) : see declaration of 'Gradebook'
    I've tried using the #indef, endif, method but i wasnt exactly sure how to use that and it didnt work out Can someone help me out here???

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    [EDIT: BOGUS POST; SEE 4th ONE INSTEAD]
    I've tried using the #indef, endif, method but i wasnt exactly sure how to use that and it didnt work out. Can someone help me out here???
    It seems you've correctly identified the problem.

    So, after that, this will definitely help.
    Last edited by manasij7479; 08-20-2011 at 04:09 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also some advice:
    - Improve your indentation. Visual Studio's default indentation is good. If you don't know how to indent properly, then let it indent for you.
    - Don't use "using namespace x" in headers. Use fully qualified types (ie std::string instead of string). You should easily find a why with a simple google search.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    This isn't a problem with inclusion guards, though it's a good idea to have them anyway. The problem is that you're "redefining" your class in the .cpp file. You should remove the public/private identifiers, the private members, and the "class Gradebook" bit from the .cpp file and you should stop getting that error.

    For example, this is how it should look:

    Code:
    // Example.h File
    class Example
    {
    public:
         int Function();
    private:
         int m_data;
    };
    Code:
     // Example.cpp file
    
    #include "Example.h"
    
    int Example::Function()
    {
         // ...
         return m_data;
    }

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Ushakal View Post
    The problem is that you're "redefining" your class in the .cpp file.
    OMG. I can't believe I could not see it.
    Thank You very much for spotting it !

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    4
    Quote Originally Posted by Ushakal View Post
    This isn't a problem with inclusion guards, though it's a good idea to have them anyway. The problem is that you're "redefining" your class in the .cpp file. You should remove the public/private identifiers, the private members, and the "class Gradebook" bit from the .cpp file and you should stop getting that error.

    For example, this is how it should look:

    Code:
    // Example.h File
    class Example
    {
    public:
         int Function();
    private:
         int m_data;
    };
    Code:
     // Example.cpp file
    
    #include "Example.h"
    
    int Example::Function()
    {
         // ...
         return m_data;
    }
    Im sorry, but I lost you, I removed the public/ private identifiers
    Now my code looks like this
    Code:
    #include "stdafx.h"
    #include "gradebook.h"
    #include <string>
    
    	using namespace std;
     class Gradebook{ //if i omit class Gradebook, then nothing works 
    
    	Gradebook::Gradebook (string name){  //constructor statement
    	Setname(name);
    	}
    	void Gradebook::Setname(string name) {   // Mutator method
    		Coursename = name;
    	}
    	string Gradebook::Getname() {
    	return Coursename ;
    	}
    	
    	void Gradebook::DisplayMessage( ){ //display message statement
    	cout << "Welcome To The Gradebook For " << Getname() << endl ; 
    	}
    
    
    	string Coursename ;
    };
    What else should I take out? If I take out class Gradebook, nothing will work?
    BTW thanks for the replies

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include "stdafx.h"
    #include "gradebook.h"
    #include <string>
    
    	using namespace std;
     class Gradebook{ //if i omit class Gradebook, then nothing works 
    
    	Gradebook::Gradebook (string name){  //constructor statement
    	Setname(name);
    	}
    	void Gradebook::Setname(string name) {   // Mutator method
    		Coursename = name;
    	}
    	string Gradebook::Getname() {
    	return Coursename ;
    	}
    	
    	void Gradebook::DisplayMessage( ){ //display message statement
    	cout << "Welcome To The Gradebook For " << Getname() << endl ; 
    	}
    
    
    	string Coursename ;
    };
    Remove red parts.
    If it doesn't work, specify the error. "Doesn't work" doesn't tell us much!
    Last edited by Elysia; 08-20-2011 at 04:26 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    4
    0.o Tyvm
    I thought that if I only tried removing
    Code:
    class Gradebook
    forgot to remove the
    Code:
    { };
    Everything works fine now, Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  2. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  3. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  4. Replies: 1
    Last Post: 01-11-2007, 05:22 PM
  5. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM