some class problems
I can't seem to get a class or a function to load. here is an example of some code:
#include "stdafx.h"
#include "iostream.h"
int main(int argc, char* argv[])
{
void text();
return 0;
}
void text ()
{ cout<<("Hello World!");
}
very basic console application right, but it won;t work. the thing links with no errors but it just shows up with nothing on the screen. also the same if I want to load a function from a class. I don't get it. any suggestions.
PS. I know you don't need it for this program, but I want the priciple for my more advanced program. my real program is huge without these things, it is nearly 20,000 lines!
please help if you can
[edited by - Landi on October 14, 2003 8:55:54 PM]
#include "stdafx.h"#include "iostream.h"int main(int argc, char* argv[]){ text(); // <------------ Make it like this return 0;}void text (){ cout<<("Hello World!");}
#include "stdafx.h"#include "iostream.h"void text(void);int main(int argc, char* argv[]){ text(); return 0;}void text (){ cout << "Hello World!";}
You need to make a prototype for it to work.
Try this:
-UltimaX-
"You wished for a white christmas... Now go shovel your wishes!"
Try this:
#include "stdafx.h"#include "iostream.h"void text();int main(int argc, char* argv[]){ text(); return 0;}void text(){ cout<<("Hello World!\n");}
-UltimaX-
"You wished for a white christmas... Now go shovel your wishes!"
Or put the text function before the main function.
And the formatting of the cout statement should be:
cout << "Hello World" << endl;
[edited by - Plasmadog on October 14, 2003 9:41:41 PM]
And the formatting of the cout statement should be:
cout << "Hello World" << endl;
[edited by - Plasmadog on October 14, 2003 9:41:41 PM]
Basically, what they''re telling you to do what''s called forward declaring. In C/C++, you cannot use a function before its declared; same as with variables. The function must be declared at a point before its actually called. So, you either need to do this:
void text();
int main(int argc, char* argv[])
{
text();
return 0;
}
void text ()
{ cout<<("Hello World!";
}
which is forward declaring, or you can implement the entire function, like this:
void text ()
{
cout<<("Hello World!";
}
int main(int argc, char* argv[])
{
text();
return 0;
}
Functionally, the two are more or less identical.
Try this:
<CPP>
#include "stdafx.h"
#include <iostream>
void text ()
{
std::cout << "Hello World!";
}
int main(int argc, char* argv[])
{
text();
return 0;
}
</CPP>
If you''re still getting errors, what are the error messages?
<CPP>
#include "stdafx.h"
#include <iostream>
void text ()
{
std::cout << "Hello World!";
}
int main(int argc, char* argv[])
{
text();
return 0;
}
</CPP>
If you''re still getting errors, what are the error messages?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement