Returning a string from static function - c++

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

Related

Visual Studio doesn't recognize std library

I'm writing an MFC application on Visual Studio 2015 in C++. I added some code which uses members of std library and suppose to take an int and create from it a hex char* with the prefix "0x". I tried to build the project on VS 2015 and VS 2017 from two different computers and I get the same errors - VS doesn't recognize the std library. I've tied running the code on other programs (Clion) and it worked well.
When I include #include <stdlib> I get the following error:
cannot open source file "stdlib"
I've tried re-installing VS, and checked I have all the necessary extensions to support C++, but I guess there's still something missing. How can I fix it?
The code:
std::ostringstream ss;
int i = 7;
ss << std::hex << std::showbase << i;
std::string str = ss.str();
const char *output = str.c_str();
std::cout << output << std::endl;
and included the following headers:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
I get the following errors:
'Ostringstream': is not a member of 'std'
'Ostringstream': undeclared identifier
'ss': undeclared identifier
'hex': is not a member of 'std'
'showbase': is not a member of 'std'
'string': is not a member of 'std'
'string': undeclared identifier
Thank you.
I've included the headers in the wrong order. In every C++ project in Visual Studio it includes "stdafx.h" library automatically. This library contains many of the commonly used libraries such as <string> and etc. The solution was to write the includes in the following way:
#include "stdafx.h"
// other headers of the form "header.h"
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
instead of:
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
#include "stdafx.h"
// other headers of the form "header.h"
a bit more about this in this question
Thanks for everyone who tried to help, I appreciate your time and attention.

error C2061: syntax error : identifier 'string'

this is probably an include problem, i get these errors all over the code, and not only for string identifier for example error C2146: syntax error : missing ';' before identifier 'getName' and error C2146: syntax error : missing ';' before identifier 'name'
here's an example class:
#include "stdafx.h"
class participant
{
public:
participant(int id, string name);
~participant(void);
int getId();
string getName();
private:
int id;
string name;
};
here's my stdafx.h file:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include "day.h"
#include "appointment.h"
#include "extendedAppointment.h"
#include "participant.h"
#include "calendar.h"
using namespace std;
#define no_such_appointment_error 20;
#define conflicting_appointments_error 21;
#define noSuchDayError 22;
#define incorrectAppointmentError 23;
So I compiled your code as-posted without your custom header files and it worked just fine. Based on that, I am going to wager that you have a problem in one of these header files:
#include "day.h"
#include "appointment.h"
#include "extendedAppointment.h"
#include "participant.h"
#include "calendar.h"
It could be a macro, a class/struct not terminated with a semi-colon, etc. Check those out.
Lastly, a few of tangential issues:
First, using a namespace in a header file is a terrible idea. Any file that includes your header now has using namespace std; in it (this is bad). You probably don't want to include that many header files in every file that includes stdafx.h.
Secondly, once you remove that then string immediately becomes undefined (use std::string instead).
Last, why are your #defines ended with semi-colons? No need for that.

Bad STL Headers

I want to make some STL sets but when I do it tells me
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
Meaning I have not included the proper header define (#define <set>) but I have. Are sets just under a different location? The source looks like this
set<int> playerlist;
The header files are...
#include <iostream>
//#include "winsock2.h"
#include "Ws2tcpip.h"
#include <list>
//#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <string>
#include <fstream>
#include <map>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#include <set>
What you need is
#include <set>
and not
#define <set>
also before using the set write the following line before your main function
using namespace std;
By
#define <set>
I hope you mean
#include <set>
?
You also need to indicate somehow that you want the set from the std namespace. You can do that either by prefixing set with std:: everywhere that you refer to it:
set<int> playerlist;
or by using a using declaration to tell the compiler that whenever you refer to set, you mean std::set:
using std::set;
(It's also possible to write a catch-all using namespace std; declaration that says that you always want to use everything from the std namespace, but that's normally considered bad practice. I mention it only because, despite being bad practice, it's fairly common, so you may well come across code that does it.)

error C2504: 'ios' : base class undefined

I am trying my hands on sample codes from a book, and so I am not entirely sure what I may have wrong in the header file I have so far.
I keep getting the following error messages.
Error 2 error C2061: syntax error : identifier 'streambuf'
Error 1 error C2504: 'ios' : base class undefined
Error 5 IntelliSense: identifier "streambuf" is undefined
// StdAfx.h HEADER FILE
**************************
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include <iostream>
#include <strstream>
#include <iomanip>
#include <ios>
#include <stdio.h>
#include <tchar.h>
#include "targetver.h"
// Conios HEADER FILE
**************************
#include "Stdafx.h"
class Conios :virtual public ios{
protected:
public:
Conios(void);
~Conios(void);
Conios(streambuf* Buffer);
};
ios is in the std-namespace. So either use use namespace std; or extend from std::ios instead of just ios.
If you are using use namespace use it only in your implementation-files like *.cpp or *.cxx, do not write use namespace ... your header files - ever!.
And the same goes for streambuf.

stringstream was not declared in this scope

I'm having problem with stringstream.my visual studio nor linux g++ can understand stingstream. I've added sstream but it does'nt solve anything. I've worked with it before and really don't know what's up with it now?
#include <sstream>
#include <stdlib.h>
#include "SymbolTable.cpp"
#include "setjmp.h"
using namespace std;
jmp_buf *bfj;
int TOP , SP=3 ;
struct types{int int_val;float float_val;char char_val;bool bool_val;};
types DS[6400];
int main(){
...//some code here
label38 : stringstream s;
label39 : bfj = (jmp_buf *)"label65";
label40 : longjmp(*bfj,1);;
label41 : goto label43;
label42 : TOP=SP;
//some code here
}
I'm writing a compiler so the code is the output,that's why it may seams a bit odd.
If you include #include <sstream> then you must also reference the class by:
std::stringstream or declare using namespace std; before using it.
If you post more information we could provide more detailed help.
This code compiles fine for me under G++:
#include <sstream>
#include <stdlib.h>
#include "setjmp.h"
using namespace std;
jmp_buf *bfj;
int TOP , SP=3 ;
struct types{int int_val;float float_val;char char_val;bool bool_val;};
types DS[6400];
int main(){
label38 : stringstream s;
label39 : bfj = (jmp_buf *)"label65";
label40 : longjmp(*bfj,1);;
label41 : goto label43;
label42 : TOP=SP;
label43 : (void)0;
//some code here
}
The only difference is that I removed #include "SymbolTable.cpp", and added a label43.
So apparently, if it doesn't work for you, the problem is in some of the code you omitted. The //some code here parts or in SymbolTable.cpp
Of course, it also seems very suspicious that you're including a cpp file. That is most likely an error.