CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: error

  1. #1
    Join Date
    Oct 2005
    Posts
    175

    Resolved error

    hi everybody!

    I have just started learning VC++ self. No body is here to guide me. so I am feeling helpless.

    can anybody help me by telling where I am commiting the mistake.
    I have prepared a test program shown bellow:

    //folowing code is written in a file named "test.h"
    class myclass
    {
    public:
    void disp();
    };


    //following is written in different file named "test.cpp"
    #include<iostream.h>
    void myclass:isp()
    {
    cout<<"hello";
    }


    //following is written in the different file named "mytest.cpp"
    #include<iostream.h>
    #include"test.h"
    void main()
    {
    myclass *mc=new myclass;
    mc->disp();
    }

    the error is given bellow:
    c:\vc\test\test.cpp(2) : error C2653: 'myclass' : is not a class or namespace name
    testmain.cpp
    Error executing cl.exe.
    test.exe - 1 error(s), 0 warning(s)

    I am waiting
    shiv

  2. #2
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    Re: error

    you're just missing brackets on creating a new object:

    Code:
    myclass *mc=new myclass(); // This will call the default constructor.
    please note also that main returns an int and not a void.
    regards
    I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)

  3. #3
    Join Date
    Oct 2005
    Posts
    175

    Resolved Re: error

    #include<iostream.h>
    #include"test.h"
    int main()
    {
    myclass *mc=new myclass();
    mc->disp();
    return(0);
    }


    when i press the ctrl+F7, there is no error
    but when I press the ctrl+F5 then i found the following error:

    --------------------Configuration: test - Win32 Debug--------------------
    Compiling...
    test.cpp
    C:\vc\test\test.cpp(2) : error C2653: 'myclass' : is not a class or namespace name
    testmain.cpp
    Error executing cl.exe.

    test.exe - 1 error(s), 0 warning(s)

  4. #4
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: error

    hi shiv

    first of all...use code tags ....

    y would u want 2 use 2 Cpp files for this small apl....no offence against using them but....just complicating things .....instead just define the main() inside the test.cpp.....

    n shiv since u are just starting 2 learn C++ try vit simple things first ....
    like instead of declaring the object of the class dynamically......

    try this....the most simplest form...

    Code:
    void main()
    {
       myclass clasObject;
       clasObject.disp();
    }
    now the error in ur code....

    declare a diff name for the object that u have created.... if i am not wrong that shoud solve your problem..... hope this helps you

    regards

  5. #5
    Join Date
    Oct 2005
    Posts
    175

    Resolved Re: error

    thanx sreehari

    Just I am trying to understand the headerfiles that's why I made this simple application.

    I made a test.h file to declare the class myclass like this

    class myclass
    {
    public:
    void disp();
    };

    then I made a new file test.cpp to define the function used in the class
    #include<iostream.h>
    void myclass:isp()
    {
    cout<<"hello";
    }

    then I used the header file in my testmain.cpp file
    #include<iostream.h>
    #include"test.h"
    void main()
    {
    myclass mc;//=new myclass;
    mc.disp();

    }

    I tried the following also

    #include<iostream.h>
    #include"test.h"
    int main()
    {
    myclass *mc=new myclass();
    mc->disp();
    return(0);
    }

    but I found the same error
    error C2653: 'myclass' : is not a class or namespace name

    thanks

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: error

    1) in test.cpp , you need to

    #include "test.h"



    2) <iostream.h> is a pre-standard header. The standard C++ header
    is <iostream> , and the functions/classes are in namespace std

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: error

    Just aside note.
    Quote Originally Posted by sreehari
    y would u want 2 use 2 Cpp files for this small apl....no offence against using them but....just complicating things .....instead just define the main() inside the test.cpp.....
    No, it not complicates, but it simplifies things. Do not forget that every small application may grow up once...
    Last edited by ovidiucucu; January 13th, 2006 at 08:06 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  8. #8
    Join Date
    Oct 2005
    Posts
    175

    Re: error

    thanx Philip Nicoletti and ovidiucucu.

    it solved my problem.

    shiv

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: error

    Quote Originally Posted by Vaderman
    you're just missing brackets on creating a new object:

    Code:
    myclass *mc=new myclass(); // This will call the default constructor.
    please note also that main returns an int and not a void.
    regards
    Parens aren't required if there are no arguments. Having a class name and a variable name being the same probably isn't a good idea though.

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: error

    [ Moved thread ]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured