some class problems

Started by
7 comments, last by Landi 20 years, 6 months ago
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]
Advertisement
#include "stdafx.h"#include "iostream.h"int main(int argc, char* argv[]){ 	text(); // <------------ Make it like this	return 0;}void text (){	cout<<("Hello World!");} 
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
I''m sorry but that gives me errors now, I still don;t get it
#include "stdafx.h"#include "iostream.h"void text(void);int main(int argc, char* argv[]){    text();   return 0;}void text (){    cout << "Hello World!";}
Interested in being apart of a team of people that are developing a toolkit that can help anyone product an online game? Then click here http://tangle.thomson.id.au/
You need to make a prototype for it to work.

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]
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter

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?
No offence intended, but it sounds like you need to read a book on C++.
____________________________________It's got to be good if its nuclear http://www.nuclearfirework.com

This topic is closed to new replies.

Advertisement