Archived

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

some class problems

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster

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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites