Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ compiler error "was not declared in this scope"

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.

like image 237
Mike Avatar asked Jul 19 '11 14:07

Mike


2 Answers

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.

like image 118
Mark B Avatar answered Sep 21 '22 18:09

Mark B


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.

like image 37
Greg Howell Avatar answered Sep 21 '22 18:09

Greg Howell