C++ Debugging no '=' operator found in Visual Studio - c++

Visual Studio gives me this error when I try to compile my C++ project:
Severity: Error
Code: C2678
Description: binary '=': no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
Project: jr
File: c:\program files (x86)\microsoft visual studio 14.0\vc\include\utility
Line: 192
I suppose, somewhere in my project, I'm trying to change constant string.
How do I locate this? How to go about debugging such errors?
The filename and line mentioned in the error are some read-only files by Microsoft. I'd like to locate the error in my code. Here's the excerpt of code around line 192:
_Myt& operator=(const _Myt& _Right)
{ // assign from copied pair
first = _Right.first;
second = _Right.second;
return (*this);
}

You probably want to write code like:
const std::string s;
s = "";
this is why error is saying about const type : 'const std::string'
Other possibility is that you want to assign a value to a string in a const method.

Related

Template deduction error

Note: pls don't be confused with other Stackoverflow questions that have the same title/errocode -- they are mostly due to missing #include <string> but it's not the issue here.
I'm reading Pretty-print C++ STL containers question and downloaed Kerrek's code from his GitHub Project cxx-prettyprint, when I try compile it (my environment is Windows 7, Visual Studio 2013, compiled project as a Console Application "Use Multi-Byte Character Set"), hit a template deduction error.
It's a bit weird as suggested by Mike Kinghan in the comment, it does compile compiles and runs fine with the current MSVC++ : see it online.
I'm confused, there must be something wrong with my configuration. I created a blank new Windows Console Application, and added Kerrek's code in, focus on a vector of string test cast, removed other scenarios such as set/map etc:
int main(int argc, char* argv[])
{
std::vector<std::string> v;
v.push_back("hello, world");
v.push_back("2nd line");
v.push_back("last line");
std::cout << v;
}
, now hit a error:
Error 1 error C2679: binary '<<' : no operator found which takes a
right-hand operand of type
'std::vector>' (or there is no
acceptable conversion)
If I explicitly call the pretty_print::operator <<,
pretty_print::operator<<(std::cout, v);
, hit another template deduction error:
Error 1 error C2784: 'std::basic_ostream<_Elem,_Traits>
&pretty_print::operator <<(std::basic_ostream<_Elem,_Traits> &,const
pretty_print::print_container_helper
&)' : could not deduce template argument for 'const
pretty_print::print_container_helper
&' from 'std::vector>'
It only compiles if the print_container_helper is explictily created:
pretty_print::print_container_helper<std::vector<std::string>, char, ::std::char_traits<char>, pretty_print::delimiters<std::vector<std::string>, char>>
vh(v);
pretty_print::operator<<(std::cout, vh);
The full code is here: http://rextester.com/RJX76690

C2664 showing up only in release configuration

I'm trying to build a program for release, but even though it works fine in a debug configuration, I am getting this error when I use the release configuration:
1>c:\users\owner.ben-pc\documents\visual studio 2010\projects\xsp quick unpacker\xsp quick unpacker\Form1.h(217): error C2664: 'DeleteFile' : cannot convert parameter 1 from 'const char *' to 'LPCTSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
I tried adding the "#define _SECURE_SCL 0" line as per this thread, but that didn't change anything.
Here's the part of the program that the line that is throwing the error is in (the "DeleteFile(temp);" is the specific line that throws the error):
path=this->FilePathBox->Text;
if (!File::Exists(path+"\\filemaker\\start.ini"))
{
FileStream^ fs=File::Create(path+"\\filemaker\\start.ini");
delete fs;
}
else
{
marshal_context^ context=gcnew marshal_context();
String^ filepath=path+"\\filemaker\\start.ini";
const char* temp;
temp=context->marshal_as<const char*>(filepath);
DeleteFile(temp);
delete context;
}
P.S. I'm using VS 2010 Premium.

C++ Function that returns function, errors C2440, C2100 in xmemory and xrefwrap

I am getting errors like:
Error 24 error C2440: 'initializing' : cannot convert from 'std::_List_const_iterator<_Mylist>' to 'AttrValue' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xmemory 208
Error 25 error C2100: illegal indirection c:\program files (x86)\microsoft visual studio 10.0\vc\include\xrefwrap 49
Error 26 error C2296: '.*' : illegal, left operand has type 'AttrValue' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xrefwrap 49
Error 37 error C2440: 'initializing' : cannot convert from 'std::_List_const_iterator<_Mylist>' to 'AttrValue' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xmemory 208
Error 38 error C2100: illegal indirection c:\program files (x86)\microsoft visual studio 10.0\vc\include\xrefwrap 49
Error 39 error C2296: '.*' : illegal, left operand has type 'AttrValue' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xrefwrap 49
It doesn't point to my code so I dont exactly know whats wrong. But looking at MSDN docs, I was thinking the problems maybe caused by:
function<bool(AttrValue)> QueryEvaluatorPrivate::getClausePredicate(Relation clauseType, int preferedIndex) {
switch (clauseType) {
case UsesRelation:
if (preferedIndex == 0)
return &QueryEvaluatorPrivate::hasVarsUsed;
return &QueryEvaluatorPrivate::hasStmtsUsing;
case UsesPRelation:
if (preferedIndex == 0)
return &QueryEvaluatorPrivate::hasVarsUsedInProc;
return &QueryEvaluatorPrivate::hasProcsUsing;
}
}
hasVarsUsed and other has* functions are just functions that return a bool. Is there something wrong with this?
UPDATE
Following #Cameron's comment, in the output window is output. I think the offending line is output.insert(x) (last line):
...
function<bool(AttrValue)> clausePredicate = getClausePredicate(cl.type, prefered);
unordered_set<AttrValue> output;
if (prefered == pos) {
for (auto x = input.begin(); x != input.end(); ++x)
if (clausePredicate(*x))
output.insert(x);
...
But whats wrong with that? Maybe I am looking at the wrong place?
UPDATE 2
Fixed the 1st problem output.insert(x) should be output.insert(*x)... but I have
Error 6 error C2100: illegal indirection c:\program files (x86)\microsoft visual studio 10.0\vc\include\xrefwrap 49
I think the offending line is:
return &QueryEvaluatorPrivate::hasVarsUsed;
I am probably returning functions wrongly?
// function declaration
bool QueryEvaluatorPrivate::hasVarsUsed(StmtNum s)
It looks like you are trying to use a function with the wrong signature, maybe because StmtNum is a derived class of AttrValue? Here's an example to explain:
#include <functional>
struct A {};
struct B : A {};
void fa( A ) {}
void fb( B ) {}
int main()
{
std::function<void(A)> f1( &fa ); // OK
std::function<void(A)> f2( &fb ); // fails
}
In your code, I see function<bool(AttrValue)>, but the function is
bool QueryEvaluatorPrivate::hasVarsUsed(StmtNum s);
Also, this function has to be static as you can not simply mix free (or static) functions with member functions when passing around pointers to them.
x = input.begin() -> looks like x is some sort of iterator
Maybe you should do:
output.insert(*x)
instead of
output.insert(x)

Using an std::string as a key for an std::map

I would like to have an std::map (int .NET 4.0). We of course know that a map is a tree and requires an operator< that string does not define for us.
Error 24 error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 125 1 FXCMMarketDataServer
So I put my google-foo to work and found this solution:
struct StringComparerForMap
{
public:
bool operator()(const std::string x, const std::string y)
{
// Add compare logic here
}
};
...
std::map<std::string, CustomObject, StringComparerForMap> myMap;
This worked fine for a while, and now I'm encountering a bug that I believe is due to this. Somewhere deep down in the STL framework it would seem that it ignores the above definition and defaults to operator<.
Is there a way in VS2010 .NET 4.0 to use a string as the key of a map?
I understand that I can take that string and write a function to hash it to an int, but where's the fun in that?
EDIT
I will try and explain this as best I can for David. When the map uses the comparer struct, it crashes in release and fails a debug assertion in debug. The assert that fails is in xtree line 1746.
Expression: invalid operator<
|Abort| |Retry| |Ignore|
That is what leads me to believe that despite giving map a comparer, it still down certain paths defaults to operator< for comparison. The line in my code that causes this is:
CustomObject o = stringObjectMap[key];
Error 24 error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 125 1 FXCMMarketDataServer
That's what VC spits into your face when you forgot to include <string>. That header definitely defines this operator.

cvblob compile error in Visual C++ 6.0

I'm using Microsoft Visual C++ 6.0 and Microsoft Visual Studio 2008 to develop an academic computer vision project.
In this project i need to use OpenCV 1.1 (http://opencv.willowgarage.com/) and CvBlob (http://code.google.com/p/cvblob/).
I tried to compile this project with Microsoft Visual Studio 2008 and it compiles without errors.
With Visual C++ 6.0 i got a lot of errors.
OpenCV are not responsible of this behavior, because a trivial project with only OpenCV (without CvBlob) works well.
To understand the errors better I made an empty project with only the CvBlob inclusion.
I paste here a brief summary of the errors:
cvcontour.cpp(253) : error C2371: 'i' : redefinition; different basic types (and others similar to this. i solved with variable redefinition, every time)
cvcontour.cpp(318) : error C2664: 'thiscall std::vector<struct CvPoint,class std::allocator<struct CvPoint> >::std::vector<struct CvPoint,class std::allocator<struct CvPoint> >(unsigned int,const struct CvPoint &,const class std::allocator<struct CvPoint> &)' : cannot convert parameter 1 from 'class std::deque<struct CvPoint,class std::allocator<struct CvPoint> >::iterator' to 'unsigned int' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
cvtrack.cpp(278) : error C2440: 'initializing' : cannot convert from 'struct cvb::CvTrack *const ' to 'struct cvb::CvBlob *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Have you ideas on how can i solve these problems?
Thanks in advance for the help!
-------- UPDATE --------
I tried to edit and correct the code in order to elminate the three errors in my question.
The error C2664 seems to be the more difficult to cirmumvent...
I have replaced the indicted line
return new CvContourPolygon(dq.begin(), dq.end());
where CvContourPolygon is a typedef std::vector<CvPoint> CvContourPolygon;
with
deque<int>::iterator dq_it;dq_it = dq.begin();
CvContourPolygon v_tmp;
v_tmp.push_back(*dq_it);
while (dq_it != dq.end()){
v_tmp.push_back(*dq_it++);
}
First, what that i wrote is correct? Than, how can i solve the errors that occured from this?
Thank you in advance!
Errors (suppose that the first line is 318:
cvcontour.cpp(319) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::deque<struct CvPoint,class std::allocator<struct CvPoint> >::iterator' (or
there is no acceptable conversion)
cvcontour.cpp(321) : error C2664: 'push_back' : cannot convert parameter 1 from 'int' to 'const struct CvPoint &'
Reason: cannot convert from 'int' to 'const struct CvPoint'
No constructor could take the source type, or constructor overload resolution was ambiguous
cvcontour.cpp(322) : error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::deque<struct CvPoint,class std::allocator<struct CvPoint> >::iterator' (or there is no acceptable conversion)
cvcontour.cpp(322) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
-------- UPDATE2 --------
This code seems to work correctly!
deque<CvPoint>::iterator dq_it;
dq_it = dq.begin();
CvContourPolygon v_tmp;
for (dq_it = dq.begin(); dq_it != dq.end(); ++dq_it){
v_tmp.push_back(*dq_it);
}
//return new CvContourPolygon(dq.begin(), dq.end());
return &v_tmp;
C2371 - VC6 was sloppy with scope of local variables. Should be able to fix this by making the code use variable names unambiguously.
C2664 - looks like failure to initialize a vector using deque iterators - wrong overload on vector::vector() being called? Probably have to work around this by manually copying the deque elements to the new vector somehow.
C2440 - check the objects are compatible (VS2008 seems to think so) and add the appropriate cast.
EDIT:
Shouldn't your code look like this?
deque<CVPoint>::iterator dq_it;dq_it = dq.begin();
CvContourPolygon v_tmp;
for (dq_it = dq.begin(); dq_it != dq.end(); ++dq_it)
{
v_tmp.push_back(*dq_it);
}