7
votes

I'm getting a bizarre compiler error when trying to compile a c++ UDP client program.

g++ -o client Udp.cpp ClientMain.c -I. -lpthread

In file included from ClientMain.c:1:0:

Udp.h: In destructor ‘CUdpMsg::~CUdpMsg()’:

Udp.h:103:43: error: ‘free’ was not declared in this scope

Udp.h: In member function ‘void CUdpMsg::Add(in_addr_t, const void*, size_t)’:

Udp.h:109:34: error: ‘malloc’ was not declared in this scope

Udp.h:109:41: error: ‘memcpy’ was not declared in this scope

ClientMain.c: In function ‘int main(int, char**)’:

ClientMain.c:28:57: error: ‘memcpy’ was not declared in this scope

ClientMain.c:29:61: error: ‘printf’ was not declared in this scope

ClientMain.c:30:17: error: ‘stdout’ was not declared in this scope

ClientMain.c:30:23: error: ‘fflush’ was not declared in this scope

ClientMain.c:34:68: error: ‘printf’ was not declared in this scope

ClientMain.c:35:17: error: ‘stdout’ was not declared in this scope

ClientMain.c:35:23: error: ‘fflush’ was not declared in this scope

ClientMain.c:37:30: error: ‘usleep’ was not declared in this scope

I have the following declared at the beginning of my cpp file.

#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <cstdlib> 
#include <string>
#include <stdlib.h>
#include <cstring>

#include <errno.h>

functions like 'memcpy' should be declared in string.h... I have it (and string and cstring) all declared, and I'm still getting these compiler errors. Does anyone have a clue why this is happening? Thanks.

4
You say you have those includes in your "cpp" file, but the errors are in ClientMain.c (note .c, not .cpp)?Georg Fritzsche
I think you need to include some of those files in UDP.hDjole
are you specify std namespace for this function callstriclosan
@triclosan: You don't need to specify std::malloc if you're including the C headers (i.e. the .h ones)MSalters
@MSalters: i agree. but it's not good practice including both <stdlib.h> and <cstdlib>.triclosan

4 Answers

5
votes

If you have multiple files, then you need the appropriate includes in each file. Also maybe its not within namespace?

8
votes

Your Udp.h file also needs to include the needed system headers. Additionally, since you use cstring and cstdlib as your includes, you'll need to qualify all the C-library functions with std:: since they aren't automatically imported into the global namespace by those headers.

7
votes

Mark B covered all the exact causes of your error. I just want to add that you should try not to mix the two flavors of C headers in a single cpp file (#include <cHEADER> vs #include <HEADER.h>).

The #include <cHEADER> variety brings all of the included declarations into the std:: namespace. The #include <HEADER.h> files include declarations do not. It is annoying to maintain code when you need std::malloc() but ::strncpy(). Pick one approach for each file or, more preferably, one approach for your entire project.

As a separate issue, you've encountered a situation in which a header does not itself include everything it needs. This can be annoying to debug because bugs can appear or disappear depending on include ordering.

If you create a header/cpp pair, always make the matched header the first include in the cpp file, this will guarantee that the header is complete and can stand on its own. If you create a standalone header that needs no implementation, you can still create an empty .cpp to test the header for include completeness, or just run the header through your compiler by itself. Doing this with every header you create will prevent headaches like your current one.

0
votes

A cleaner solution is probably to move the implementation of CUdpMsg::~CUdpMsg from udp.h to udp.cpp, and similarly any function that is giving you such errors. Only define functions in headers if they're really simple (e.g. getters).