-
January 13th, 2006, 07:01 AM
#1
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
-
January 13th, 2006, 07:04 AM
#2
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)
-
January 13th, 2006, 07:14 AM
#3
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)
-
January 13th, 2006, 07:17 AM
#4
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
-
January 13th, 2006, 07:29 AM
#5
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
-
January 13th, 2006, 07:52 AM
#6
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
-
January 13th, 2006, 08:01 AM
#7
Re: error
Just aside note.
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.
-
January 13th, 2006, 08:09 AM
#8
Re: error
thanx Philip Nicoletti and ovidiucucu.
it solved my problem.
shiv
-
January 13th, 2006, 08:11 AM
#9
Re: error
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.
-
January 13th, 2006, 08:20 AM
#10
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|