Question: Undeclared Function When Declared in Separate File

Hello and thanks ahead of time for helping me out on this.

I am a very new c++ programmer and I am just copying and pasting the initialization code (with hopes that I'll never need to know what it actually does) for winsock so I can make a socket server. I have all my linkers set up properly.

When I take all the code and put it in one .cpp file, everything works smoothly. I want to have clean code though so I made some functions in separate files to handle different parts of the process.

I understand the problem: I have a function declared in one of my files. It gets executed from one of my other functions. When I make reference to the returned value of the function, I get a syntax error that says that the function is not declared.

The syntax error is at line 11 of socketserver.cpp. I have had this problem before so please ignore the fact that I'm approaching a task that is too difficult. I understand a lot of things that I shouldn't and I am missing a lot of things that I should know.

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//headerserv.h - This works fine.
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#define DEFAULT_PORT "1232"
#endif

#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>

#pragma comment(lib, "Ws2_32.lib")
#pragma once

using namespace std;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//setaddrinfo.cpp
#include "headerserv.h"

int setaddrinfo ()
{

struct addrinfo *result = NULL, *ptr = NULL, hints;

ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;

// Resolve the local address and port to be used by the server
int iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
    printf("getaddrinfo failed: %d\n", iResult);
    WSACleanup();
    return 1;
}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//socketserver.cpp (main)
#include "headerserv.h"

int main(){
    int wsainit (); 
    int setaddrinfo ();
    
    SOCKET ListenSocket = INVALID_SOCKET;
    
    // Create a SOCKET for the server to listen for client connections
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); //Here, it says that result is undefined but it is defined in setaddrinfo.cpp
    
    if (ListenSocket == INVALID_SOCKET) {
    printf("Error at socket(): %ld\n", WSAGetLastError());
    freeaddrinfo(result);
    WSACleanup();
    return 1;
    }
    
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//wsainit.cpp - This works fine as well
#include "headerserv.h"

int wsainit (){
    WSADATA wsaData;
    
    int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
    printf("WSAStartup failed: %d\n", iResult);
    return 1;
    }
    return 0;
}
In order to use a function that is defined in one .cpp file in another .cpp file, it must be declared in the other .cpp file but not defined. The compiler allows this, and then the link will match up the functions. If the compiler can't even find the function, it'll never get to the linker in the first place.
EG:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//A.cpp
int MyFunc(const int& hello, bool world) //definition:
{
  if(world)
  {
    return(hello * 2);
  }
  return(hello / 2);
}

//B.cpp
int MyFunc(const int&, bool); //declaration

void DoSomething()
{
  int q = MyFunc(72, false);
}


Though normally, the function declaration/prototype is put in a header that is included in all .cpp files to save hassle. The definition must only be in one.cpp file, thogh, or the linker will yell at you for multiple definitions.
Last edited on
This makes a lot of sense. Thank you very much for the help. It now works well... At least it compiles now I should say. I can continue working on the code :).
Topic archived. No new replies allowed.