The following code compiles in Visual Studio 2010 but fails to compile in the Visual Studio 2012 RC.
#include <string>
// Windows stuffs
typedef __nullterminated const wchar_t *LPCWSTR;
class CTestObj {
public:
CTestObj() {m_tmp = L"default";};
operator LPCWSTR() { return m_tmp.c_str(); } // returns const wchar_t*
operator std::wstring() const { return m_tmp; } // returns std::wstring
protected:
std::wstring m_tmp;
};
int _tmain(int argc, _TCHAR* argv[])
{
CTestObj x;
std::wstring strval = (std::wstring) x;
return 0;
}
The error returned is:
error C2440: 'type cast' : cannot convert from 'CTestObj' to 'std::wstring'
No constructor could take the source type, or constructor overload resolution was ambiguous
I've already realized that commenting out either of the conversion operators fixes the compile problem. I just want to understand:
What's going on under the hood to cause this
Why this compiles in VS2010 and not in VS2012? Is it because of a C++11 change?
If I'm understanding the logic under the hood, the operator overload is trying to copy the code and the object every time you cast. Therefore, you need to return it as a reference instead of attempting to return a new object based on the field. The line:
operator std::wstring() const { return m_tmp; }
should be:
operator std::wstring&() { return m_tmp; }
The following compiles and runs as expected.
#include <string>
// Windows stuffs
typedef __nullterminated const wchar_t *LPCWSTR;
class CTestObj {
public:
CTestObj() {m_tmp = L"default";};
operator LPCWSTR() { return m_tmp.c_str(); } // returns const wchar_t*
operator std::wstring&() { return m_tmp; } // returns std::wstring
protected:
std::wstring m_tmp;
};
int main()
{
CTestObj x;
std::wstring strval = (std::wstring) x;
wprintf(L"%s\n", strval.c_str());
return 0;
}
Related
I know there are a couple other questions on this specific question, but nothing that I can find on it seems to work, so I'm posting my specific code.
Here is the code:
#ifndef __MEMORY_TRACKER_H__
#define __MEMORY_TRACKER_H__
#include <unordered_map>
namespace cige
{
namespace memory
{
class CIGE_API MemoryTracker
{
protected:
typedef struct AllocRecord
{
size_t bytes;
std::string filename;
size_t line;
std::string func;
AllocRecord() :
bytes(0), line(0)
{ }
AllocRecord(size_t sz, const char* file, size_t ln, const char* fun) :
bytes(sz), line(ln)
{
if (file)
filename = file;
if (fun)
func = fun;
}
} AllocRecord;
std::string m_leakFileName;
bool m_dumpToConsole;
typedef std::unordered_map<void*, AllocRecord> AllocMap;
AllocMap m_allocationMap;
size_t m_totalAllocations;
bool m_recordEnable;
protected:
void reportLeaks();
MemoryTracker() :
m_leakFileName("CIGEMemory.log"), m_dumpToConsole(true), m_totalAllocations(0), m_recordEnable(true)
{ }
public:
void setReportFileName(const std::string& name)
{
m_leakFileName = name;
}
const std::string& getReportFileName() const
{
return m_leakFileName;
}
void setReportToConsoleOutput(bool b)
{
m_dumpToConsole = b;
}
bool getReportToConsoleOutput() const
{
return m_dumpToConsole;
}
void setRecordEnable(bool b)
{
m_recordEnable = b;
}
bool getRecordEnable() const
{
return m_recordEnable;
}
size_t getTotalMemoryAllocated() const
{
return m_totalAllocations;
}
void _recordAlloc(void* ptr, size_t sz, const char* file = nullptr, size_t ln = 0, const char* fun = nullptr);
void _recordDealloc(void* ptr);
~MemoryTracker()
{
reportLeaks();
}
static MemoryTracker& get();
};
}
}
#endif // __MEMORY_TRACKER_H__
I'm getting: variable 'cige::memory::CIGE_API cige::memory::MemoryTracker' has initializer but incomplete type at the line with the class declaration. I've looked all over and I cant find any answers that have fixed this issue.
I'm also having the error expected '}' or ',' or ';' before 'protected' at the line with protected, right above the struct.
Any help with either of these two errors would be appreciated.
EDIT: CIGE_API is defined in a separate file (which is included), as __declspec(dllexport).
EDIT2: I fixed my problem (see the answer below). It was basically just Code::Blocks derping out pretty bad.
Looks like CIGE_API is not defined. So compiler try to resolve it like variable declaration class Type Variable {initializer-list}, where Type is CIGE_API and Variable is MemoryTracker.
In other words, syntactically you're predeclaring CIGE_API type and creating variable of this type instead of defining a class.
The definition
class CIGE_API MemoryTracker { ... };
is not valid C++. I guess CIGE_API is a macro defined to an implementation specific extension, but you didn't include the corresponding header which defines that macro.
Ok I ended up fixing my own problem. Code::Blocks wasn't properly finding files that were in my project (about the third time this has happened).
In entirely unrelated news, does anyone know another cross-platform IDE that works well for C++? (I already know about Eclipse).
I have some code which "works" in VS2008 in that it will build and run and do what I expect. However I have a build error in VS2013, I've managed to reproduce it with just this code:
#include "stdafx.h"
#include <Windows.h>
#include <vector>
template < class T >
class SignatureScanner
{
public:
SignatureScanner(const BYTE* aPattern, const char* aMask, int aExpectedMatches = 1, int aOffSetToFuncStart = 0)
{
}
const std::vector<T> Funcs() const
{
return iFuncs;
}
private:
std::vector<T> iFuncs;
};
// Workaround/hack to define a thiscall function
static void __fastcall fake_this_call_func(void *thisPtr, void* not_used_hack, int aParam)
{
printf("this [%x] param [%d]\n", thisPtr, aParam);
}
// type_traits(396): error C3865: '__thiscall' : can only be used on native member functions
int _tmain(int argc, _TCHAR* argv[])
{
typedef void(__thiscall* myFuncPtr)(int thisPtr, int aParam);
// I use this in some hook code to find a function and redirect it to fake_this_call_func
// which works when it builds
SignatureScanner< myFuncPtr > p(0, 0);
return 0;
}
I want to know why did 2008 not have an error when 2013 does, and how should I go about resolving this error? It only needs to build for Windows XP 32-bit - so platform specific hacks are OK.
In the code below, the line
const char * const * eNames (names+cntNames);
results in a C2061 error in Visual Studio 2008:
syntax error : identifier 'identifier' -
The compiler found an identifier where it wasn't expected.
Make sure that identifier is declared before you use it.
An initializer may be enclosed by parentheses.
To avoid this problem, enclose the declarator in parentheses or make it a typedef.
This error could also be caused when the compiler detects an expression as a class
template argument; use typename to tell the compiler it is a type.
If I change to
const char * const * eNames = names+cntNames;
it doesn't complain. Is this a compiler bug? If not, why the complaint?
My About box says: Version 9.0.30729.1 SP
My colleague with GCC does not see this error.
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>
namespace ns1 {
struct str_eq_to
{
str_eq_to(const std::string& s) : s_(s) {}
bool operator()(const char* x) const { return s_.compare(x)==0; }
const std::string& s_;
};
static bool getNameIndex(const char * const * names, size_t cntNames, const std::string& nm, int &result)
{
const char * const * eNames (names+cntNames); //VS2008 error C2061: syntax error : identifier 'names'
const char * const * p = std::find_if(names, eNames, str_eq_to(nm));
if(p==eNames) return false;
result = p-names;
return true;
}
} //namespace ns1
int main() {
const char * const names[] = {"Apple", "Orange","Plum"};
std::string str = "Plum";
int res;
ns1::getNameIndex(names, 3, str, res);
std::cout << str << " is at index " << res << std::endl;
return 0;
}
This is most definitely a compiler bug. Witness:
extern char** a;
typedef char* cp;
char** c(a); // error
cp* c1(a); // no error
char** c2(c1); // error
cp* n(0); // no error
char** n2(0); // error
SOLVED: Restarted Visual Studio
I'm working on a project for school involving the STL list. and getting this error with xmemory. I'm just trying to build the solution at this point, butxmemory is killing me
Error 1 error C2664: 'GroceryStoreItem::GroceryStoreItem(GroceryStoreItem &)' : cannot convert parameter 1 from 'std::string' to 'GroceryStoreItem &' d:\microsoft visual studio 10.0\vc\include\xmemory 208
Here's my header
#include <string>
#include <sstream>
#include<iostream>
#include <iterator>
#include <list>
using namespace std;
//
//*****************************************************************
// USER DEFINED DATA TYPES
//
class GroceryStoreItem
{
friend ostream & operator<< (ostream &out, const GroceryStoreItem &RHS);
public:
GroceryStoreItem();
GroceryStoreItem(string Name, double cost, string location);
GroceryStoreItem(GroceryStoreItem & GroceryStoreItemCCIn);
GroceryStoreItem & operator= (const GroceryStoreItem &RHS);
string ReturnItemName();
string ReturnLocation();
double ReturnCost();
private:
string ItemName;
string Location;
double Cost;
};
and the implementation
#include "Grocery_Item.h"
using namespace std;
//*****************************************************************
// Grocery Item Constructors
//*****************************************************************
GroceryStoreItem::GroceryStoreItem()
{
ItemName = "default";
Location = "aisle 1";
Cost = 0.0;
}
GroceryStoreItem::GroceryStoreItem(string InName, double InCost, string InLocation)
{
ItemName = InName;
Location = InLocation;
if(InCost >= 0.0f)
{
Cost = InCost;
}
else
{
Cost = 0.0f;
}
}
GroceryStoreItem::GroceryStoreItem(GroceryStoreItem & GroceryStoreItemCCIn) //Copy Constructor
{
ItemName=GroceryStoreItemCCIn.ItemName;
Location=GroceryStoreItemCCIn.Location;
Cost=GroceryStoreItemCCIn.Cost;
}
edit
xmemory error in the last line of
template<class _Other>
void construct(pointer _Ptr, _Other&& _Val)
{ // construct object at _Ptr with value _Val
::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Other>(_Val));
You need to make your copy constructor argument const
GroceryStoreItem::GroceryStoreItem(const GroceryStoreItem& GroceryStoreItemCCIn)
Also it's generally better to use initialisation not assignment in your copy constructor
GroceryStoreItem::GroceryStoreItem(const GroceryStoreItem& rhs) :
ItemName(rhs.ItemName),
Location(rhs.Location),
Cost(rhs.Cost)
{
}
Finally (and this is the most important lesson of all) because you have done the right thing and used std::string internally in your class you don't actually need a copy constructor at all. The compiler generated default will do the right thing anyway. So I would actually delete your copy constructor, this will also fix the error.
Same argument for your assignment operator, delete that too.
I closed Visual Studio, started a new project and pasted my CPPs and Headers into the new project, and it compiled and worked. Not the ideal answer, but it worked.
I'm using the C++11 system_error error code library to create a custom error class for a library I'm making. I've done this before with boost::error_code, but I can't quite it get it working with std::error_code. I'm using GCC 4.6.
Basically, I've laid out all the boilerplate code to create an error class, an error_category, and the conversion routines in the STD namespace to convert my custom enums into an std::error_code object:
namespace mylib
{
namespace errc {
enum my_error
{
failed = 0
};
inline const char* error_message(int c)
{
static const char* err_msg[] =
{
"Failed",
};
assert(c < sizeof(err_msg) / sizeof(err_msg[0]));
return err_msg[c];
}
class my_error_category : public std::error_category
{
public:
my_error_category()
{ }
std::string message(int c) const
{
return error_message(c);
}
const char* name() const { return "My Error Category"; }
const static error_category& get()
{
const static my_error_category category_const;
return category_const;
}
};
} // end namespace errc
} // end namespace mylib
namespace std {
inline error_code make_error_code(mylib::errc::my_error e)
{
return error_code(static_cast<int>(e), mylib::errc::my_error_category::get());
}
template<>
struct is_error_code_enum<mylib::errc::my_error>
: std::true_type
{ };
The problem is, implicit conversion between my error code enums and std::error_code objects doesn't seem to be working, so I can't for example try and compare an instance of std::error_code with enum literals:
int main()
{
std::error_code ec1 = std::make_error_code(mylib::errc::failed); // works
std::error_code ec2 = mylib::errc::failed; // doesn't compile
bool result = (ec2 == mylib::errc::failed); // doesn't compile
}
The expression ec2 == mylib::errc::failed won't compile - I have to say ec2 == std::make_error_code(mylib::errc::failed).
The error the compiler emits is:
In file included from test6.cc:3:0:
/usr/include/c++/4.6/system_error: In constructor ‘std::error_code::error_code(_ErrorCodeEnum, typename std::enable_if<std::is_error_code_enum<_ErrorCodeEnum>::value>::type*) [with _ErrorCodeEnum = mylib::errc::my_error, typename std::enable_if<std::is_error_code_enum<_ErrorCodeEnum>::value>::type = void]’:
test6.cc:70:37: instantiated from here
/usr/include/c++/4.6/system_error:127:9: error: cannot convert ‘mylib::errc::my_error’ to ‘std::errc’ for argument ‘1’ to ‘std::error_code std::make_error_code(std::errc)’
And here's an Ideone link.
So, why isn't this working? Do I need additional boilerplate code to enable mylib::errc::my_error enums to be implicitly convertible to std::error_code? I thought that the specialization of std::make_error_code takes care of that?
You have to move error_code make_error_code(mylib::errc::my_error e) function from std to your error namespace (mylib::errc). Please check http://ideone.com/eSfee.