C++ compiler error “was not declared in this scope”

ccompiler-errorsg++

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.

Best Answer

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.

Related Topic