error c2011 when creating multiple classes

Aug 27, 2011 at 5:48pm
I've searched the forum for a similar error with no success. Here's the code:

this is the main class "test.cpp."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>;
#include "class1.h"

using namespace std;


int main(){

        class1 test;
        int x,y;

        cin >> x;

        cin >> y;
        test.setSomething(x,y);
        test.printSomething();

    return 0;

}


class1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef CLASS1_H
#define CLASS1_H

class class1{

    public:

        void setSomething(int, int);
        void printSomething();

    private:

        int x;
        int y;



};

#endif


class1.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "class1.h"

using namespace std;

class class1{


            void class1::setSomething(int x, int y){

                class1::x = x;
                class1::y = y;

            }//end class1

            void class1::printSomething(){

                cout << "the sum is: " << (x+y) << " .\n";

            }

};


I receive error c2011, the full error below. There are other errors as well.

D:\...\proj1\test.cpp|1|warning C4067: unexpected tokens following preprocessor directive - expected a newline|
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xlocale|342|warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc|
D:\...\proj1\test.cpp|6|error C2011: 'class1' : 'class' type redefinition|
D:\...\proj1\test.cpp|27|error C2079: 'test' uses undefined class 'class1'|
D:\...\proj1\test.cpp|33|error C2228: left of '.setSomething' must have class/struct/union|
D:\...\proj1\test.cpp|34|error C2228: left of '.printSomething' must have class/struct/union|
||=== Build finished: 4 errors, 2 warnings ===|


Any help would be greatly appreciated.
Aug 27, 2011 at 5:57pm
class1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "class1.h"

#include <iostream>

using namespace std;



            void class1::setSomething(int x, int y){

                class1::x = x;
                class1::y = y;

            }//end class1

            void class1::printSomething(){

                cout << "the sum is: " << (x+y) << " .\n";

            }
Aug 27, 2011 at 6:26pm
Thanks so much! However, I receive the errors:


D:\...\proj1\test.cpp|1|warning C4067: unexpected tokens following preprocessor directive - expected a newline|
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xlocale|342|warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc|
test.obj||error LNK2019: unresolved external symbol "public: void __thiscall class1::printSomething(void)" (?printSomething@class1@@QAEXXZ) referenced in function _main|
test.obj||error LNK2019: unresolved external symbol "public: void __thiscall class1::setSomething(int,int)" (?setSomething@class1@@QAEXHH@Z) referenced in function _main|
D:\...\proj1\test.exe||fatal error LNK1120: 2 unresolved externals|
||=== Build finished: 3 errors, 2 warnings ===|


One error in specific I'm curious about is the error LNK2019.
Aug 27, 2011 at 6:49pm
#include <iostream>; // Khm-khm
On my VS2008 it compiles and works. Try to remake the project (delete project and make new).
Aug 27, 2011 at 7:15pm
I created a brand new project in VS2008 with the same code as above (adding #include <iostream>;) and receive the same C2011 error. Here's the error according to VS2008

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>class1.cpp
1>d:\...\class1.cpp(5) : error C2011: 'class1' : 'class' type redefinition
1>        d:\...\class1.h(4) : see declaration of 'class1'
1>Build log was saved at "file://d:\...\BuildLog.htm"
1>test - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Aug 27, 2011 at 7:20pm
I meant: don't write ';' after #include <iostream>.
Try this:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "class1.h"

using namespace std;


int main(){

        class1 test;
        int x,y;

        cin >> x;

        cin >> y;
        test.setSomething(x,y);
        test.printSomething();

    return 0;

}


class1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "class1.h"

#include <iostream>

using namespace std;



            void class1::setSomething(int x, int y){

                class1::x = x;
                class1::y = y;

            }//end class1

            void class1::printSomething(){

                cout << "the sum is: " << (x+y) << " .\n";

            }


class1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #pragma once

class class1{

    public:

        void setSomething(int, int);
        void printSomething();

    private:

        int x;
        int y;



};
Aug 27, 2011 at 7:48pm
Still receiving the same error, c2011.

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>class1.cpp
1>d:\...\class1.cpp(7) : error C2011: 'class1' : 'class' type redefinition
1>        d:\...\class1.h(3) : see declaration of 'class1'
1>d:\...\class1.cpp(24) : fatal error C1004: unexpected end-of-file found
1>Generating Code...
1>Build log was saved at "file://d:\...\Debug\BuildLog.htm"
1>test - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Last edited on Aug 27, 2011 at 7:49pm
Aug 27, 2011 at 7:53pm
I recompiled this code (from my last post) and all successed. I don't know, why you have problems.
Sep 6, 2011 at 2:06am
Found the problem, just a minor typo. Worked when I copied and pasted everything exactly as it was. Thanks so much!!
Topic archived. No new replies allowed.