Public Group

Archived

This topic is now archived and is closed to further replies.

some class problems

This topic is 5394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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]

Share on other sites
#include "stdafx.h"#include "iostream.h"int main(int argc, char* argv[]){ 	text(); // <------------ Make it like this	return 0;}void text (){	cout<<("Hello World!");}

Share on other sites
I''m sorry but that gives me errors now, I still don;t get it

Share on other sites
#include "stdafx.h"#include "iostream.h"void text(void);int main(int argc, char* argv[]){    text();   return 0;}void text (){    cout << "Hello World!";}

Share on other sites
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!"

Share on other sites
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]

Share on other sites

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.

Share on other sites
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?

Share on other sites
No offence intended, but it sounds like you need to read a book on C++.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631774
• Total Posts
3002274
×