C++ code that look perfectly fine causes error - c++

I ran this code on Visual studio Professional 2013 and it worked, but it doesn't work on Visual Studio Express 2013.
#include "cli_tcp.h"
cli_tcp::cli_tcp()
// CLIENT TCP PROGRAM
// Revised and tidied up by
// J.W. Atwood
// 1999 June 30
char* getmessage(char *);
/* send and receive codes between client and server */
/* This is your basic WINSOCK shell */
#pragma comment( linker, "/defaultlib:ws2_32.lib" )
#include <winsock2.h>
#include <ws2tcpip.h>
#include <winsock.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <windows.h>
Error 1 error C2144: syntax error : 'char' should be preceded by ';'
Error 2 error C2761: '{ctor}' : member function redeclaration not allowed
3 IntelliSense: expected a '{'
I didn't post the entire code, because it was irrelevant. The only line that seems to be wrong is: char* getmessage(char *);

You are simply not doing what you should when you state "cli_tcp::cli_tcp()".
The compiler is expecting you to provide the implementation for the constructor of the class, and you are not providing anything.
this
cli_tcp::cli_tcp()
should be
cli_tcp::cli_tcp() { /*code*/ }
or else it won't work anywhere.

Related

Returning a string from static function

I have two files: DateTime.h and DateTime.cpp which are shown below:
DateTime.h
class DateTime
{
public:
static string getCurrentTimeStamp();
};
DateTime.cpp
#include "stdafx.h"
#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
My compiler (Visual Studio 2012) is spitting out errors the moment I have the function getCurrentTimeStamp() return an std::string object. The errors all point to syntactic problems but none are clear. Does anyone understand why this may be the case?
Update: Here are (some of) the errors.
Error 6 error C2064: term does not evaluate to a function taking 0
arguments c:\users\anthony\documents\code\consoleapplication1\datetime.cpp 21 1 ConsoleApplication1
Error 1 error C2146: syntax error : missing ';' before identifier
'getCurrentTimeStamp' c:\users\anthony\documents\code\consoleapplication1\datetime.h 5 1 ConsoleApplication1
Error 7 error C2146: syntax error : missing ';' before identifier
'getCurrentTimeStamp' c:\users\anthony\documents\code\consoleapplication1\datetime.h 5 1 ConsoleApplication1
Error 5 error C2371: 'DateTime::getCurrentTimeStamp' : redefinition;
different basic
types c:\users\anthony\documents\code\consoleapplication1\datetime.cpp 10 1 ConsoleApplication1
When trying to diagnose an issue with a header file, especially a simple one like this, step 1 is to try and see what the compiler sees.
#include is a pre-processor directive, so the compiler doesn't see it, instead the compiler sees the pre-processed output of the file that you're trying to include.
So your code would look something like this:
#include "stdafx.h"
//#include "DateTime.h"
class DateTime
{
public:
static string getCurrentTimeStamp();
};
//#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
http://rextester.com/MODV66772
When I try and compile this on RexTester's online Visual Studio, I get very different errors, telling me that your stdafx.h isn't empty.
If I modify the code a little:
//#include "stdafx.h"
//#include "DateTime.h"
#include <string>
class DateTime
{
public:
static std::string getCurrentTimeStamp();
};
//#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
This now compiles without the errors/warnings you are reporting: http://rextester.com/PXE62490
The changes:
include in the header file, since the header depends on it,
use std::string instead of string,
The C++ compiler is a single-pass compiler, so the header file can't know that you are intending to do using namespace std later, and even if it did, it's a terrible practice because the std namespace is densely populated.
If you absolutely can't do with typing std:: all over the place, try using the names you need, e.g.
using std::string; // string no-longer needs to be std::string

WinSock2.h Compilation Errors

I am trying to run a project using Visual studio 2003. But I am getting lot of compilation errors similar to the following.
The errors are pointing to WinSock2.h file. I am copying couple of code snippets from WinSock2.h file and the corresponding errors
typedef struct fd_set {
u_int fd_count; /* how many are SET? */
SOCKET fd_array[FD_SETSIZE]; /* an array of SOCKETs */
} fd_set;
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinSock2.h(114): error C2065: 'fd_set' :
undeclared identifier
struct sockaddr {
u_short sa_family; /* address family */
char sa_data[14]; /* up to 14 bytes of direct address */
};
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinSock2.h(109): error C2143: syntax
error : missing ';' before '{'
The ws2_32.lib file is added to "Configuration properties - Linker - Input - Additional Dependencies". The build configuration platform is win32.
Thanks in advance for your help.
a typical basic Winsock Application with the good order of header files can be found here:
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
return 0;
}
The order of including header files is important

C4430 and C2146 Visual Studio errors

I have searched through many posts on here and cannot seem to locate a solution to my problem. I am getting two errors when I try to compile my program, both of them are coming from one of my header files. Here are the errors:
Error 1 error C2146: syntax error : missing ';' before identifier 'datastore'
AND
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
In my cpp file I have scope resolution operators and I don't have any squiggly red lines under anything. Also the program compiled ONCE and then I saved it and reopened the program and it gave me these errors. So I think I originally "tricked" the compiler or something weird. So any help would be awesome!
#ifndef INTERNET_H
#define INTERNET_H
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sstream>
#include "Wininet.h"
#include "Internet.h"
#include "ForexPrices.h"
using namespace std;
class Internet
{
private:
ForexPrices datastore;
BOOL bResult;
char *chPtr0,
*chPtr1,
*chPtr2;
DWORD dw1,
dw2,
dwIndex;
HINTERNET hInet, hRequest;
HINTERNET h_Inet;
char ch_Buffer[4096],
ch_Line[256];
std::ofstream of_OutFile;
public:
Internet();
void openFile();
void internetCheckConnection();
HINTERNET internetopen();
HINTERNET internetconnect();
void internetclose();
void closeFile();
char* grabMargin();
double grabDailyAverageLine();
void setcurrency(char *currencyfiller1);
};
#endif
[error C2146: syntax error : missing ';' before identifier 'datastore'] is a hint that the class before 'datastore' is unknown, which leads to your next error.
[error C4430: missing type specifier - int assumed. Note: C++ does not support default-int] comes as a result of the first error. Because the compiler doesn't know what your ForexPrices class is, it is trying to use something else (I'm no expert on default-int). This is not supported and so you see this error instead.
For some reason your ForexPrices class is unknown. I see that you included the file above, ForexPrices.h. I would make sure that the name of your class is exactly the same in your header file as it is used here. Also make sure it isn't declared in a namespace that you haven't included. If so, you'll need another using statement or reference the class in the namespace (YourNamespace::ForexPrices). It's good practice not too always trust the "squigglies" I think. Visual studio can sometimes goof at least until your solution is fully parsed, but this is more of a problem on very large projects where parsing takes some time.

C++ compilation error when adding windows.h

I need to write small tool in C++
I've never used C++ as programming language before (I have couple years of Java dev experience) and .NET
I've started a new project in VS , when I am adding in my Header file of my class
#include <windows.h> I am getting the following error:
Error 1 error C2143: syntax error : missing ';' before '*' c:\program files\microsoft sdks\windows\v7.0a\include\servprov.h 96 1 CppLog
For now my class even doesn't have any real functions and looks like
in header
class TheTool
{
public :
void Foo();
};
in cpp
void TheTool::Foo(){};
and the project doesn't get compiled.
plz any suggestions ? Maybe a compiler doen't set up good ?
this is how the Header file looks like
#pragma once
#include "stdafx.h"
#include <stdio.h>
//#include <Windows.h>
//#include <winuser.h>
//#include <windowsx.h>
//#include <time.h>
class TheTool
{
public :
void Foo();
};
When I am uncommenting the include I am starting to get this compilation error.
BTW , how can i know the compiller setting ?

error C2065: 'socklen_t' : undeclared identifier

The whole error is:
Error 1 error C2065: 'socklen_t' : undeclared identifier c:\users\richard\documents\visual studio 2010\projects\server\server\server.cpp 41 1 Server
This is the problematic line:
int iRcvdBytes=recvfrom(iSockFd, buff, 1024000, 0, (struct sockaddr*)&cliAddr, (socklen_t*)&cliAddrLen);
I have these headers included:
#include <winsock2.h>
#include <windows.h>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
I have also added WS2_32.lib to the linker in Visual Studio 2010.
What else could cause this problem? I'm just trying to rewrite my simple UDP program to work under Windows.
The socklen_t type is defined inside of WS2tcpip.h in windows. This is not transitively included from winsock2.h (AFAICT). You'll need to include WS2tcpip.h manually in order to use the socklen_t type.
Visual Studio can't find the type socklen_t. MSDN says that this function takes an int* as the last parameter, so cast to that.