Sign in to follow this  
ARC inc

Problems

Recommended Posts

Ok, so it's me again = ) <hope your not tired of me yet> anyways I am working on my GameServer an I've decided to put the GameServer in a console app. It's been awhile since I've done a Console and a few things have been well lost(it happens) I was running a test to make sure I remember how to make a void function and print out the information it contains, to my main.cpp and well I keep on getting errors, I am not sure what I am doing wrong. main.cpp
#include <iostream>
#include <windwos.h>
#include "corefunctions.h"

using namespace std;

int main()
{

void GameConnect();

             cout << GameConnect() << endl;

return 0;
}

corefunctions.h
#ifndef COREFUNCTIONS_H
#define COREFUNCTIONS_H

void GameConnect()

#endif
corefunctions.cpp
#include <iostream>
#include "corefunctions.h"

using namespace std;

void GameConnect();

              cout << "GameConnection test!"<< endl;

return 0;
}
I know I did something wrong it does not feel right, it's just been awhile since I've done things like this.

Share this post


Link to post
Share on other sites
Second, function prototypes end with a semicolon, not the functions themselves.


// main.cpp
...
int main()
{

void GameConnect(); // Remove the function prototype, as it's defined in your header

cout << GameConnect() << endl;

return 0;
}
...

// corefunctions.cpp

...
void GameConnect(); // Get rid of the semicolon here, and add a {

cout << "GameConnection test!"<< endl;

return 0;
}
...

#ifndef COREFUNCTIONS_H
#define COREFUNCTIONS_H

void GameConnect() // Add a semicolon here

#endif







Share this post


Link to post
Share on other sites
Quote:
Original post by ARC inc
Ok, yah now I got the problem fixed but now....

when I run the program it executes but it prints

1

thats it...I don't know what the heck happened.


ok, look at GameConnect.


void GameConnect()
{
cout << "GameConnection test!"<< endl;

return 0;
}




Notice anything wrong? You might want to look at how you define GameConnect.

Share this post


Link to post
Share on other sites
So, this works (function returns nothing, is simply called):

#include <iostream>

void addition(int, int);

int main()
{
addition(5,7);

return 0;
}

void addition(int x, int y)
{
std::cout << "This is some text\n";
std::cout << (x + y) << "\n";

return;
}



And this works (function returns an int, used in-line with cout):

#include <iostream>

int addition(int, int);

int main()
{
std::cout << addition(5,7) << "\n";

return 0;
}

int addition(int x, int y)
{
std::cout << "This is some text\n";

return x + y;
}



But this doesn't, I think it has something to do with using the stream operator on a void return type:

#include <iostream>

void addition(int, int);

int main()
{
std::cout << addition(5,7) << "\n";

return 0;
}

void addition(int x, int y)
{
std::cout << "This is some text\n";
std::cout << (x + y) << "\n";

return;
}

Share this post


Link to post
Share on other sites
From MSDN:
"When used as a function return type, the void keyword specifies that the function does not return a value"

I'm trying to help the OP by showing various working versions of an example of what he's trying to do. Calling a void function like a value-returning function obviously doesn't compile in this case.

edit: in my example case (codeblock 3), you receive "error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' "

Share this post


Link to post
Share on other sites
Quote:
Original post by ARC inc
Oh lol, forgot to mention what errors I was getting lol


I am getting errors with using namespace it says something like I need to use something before using


But you still forgot to tell us the errors you're getting. The compiler doesn't say "Oh, you need to do something like use something before using".

It gives you an exact error message, with filename, line number and even an error number so you can look the exact meaning up on MSDN.
And it does this for a reason.

It's to make it easier to diagnose the problem when you get a compile error.

So when you ask for help in diagnosing your compile errors, you may want to, you know, show the *exact* errors you're getting. Don't remove all the relevant information, and say "well, it was an error about me doing something wrong". We'd probably already guessed that part.

Anyway, what does your code look like now, after you got it to compile?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this