I just have a very basic class that gives with functions that return the winning team of a match.
here's team.cpp
class teams
{
string teamName;
string teamName2;
int score;
int score2;
public:
teams ();
void set_team1();
void set_team2();
string get_score()
{
if (score > score2)
{
return teamName;
}
else
{
return teamName2;
}
}
private:
void teams::set_team1(string teamName, int score)
{
this->teamName=teamName;
this->score=score;
}
void teams::set_team2(string teamName2, int score2)
{
this->teamName2=teamName2;
this->score2=score2;
}
};
and here's is the line where i'm getting the error in the main method. I'm trying to create a teams object.
firstTeam.set_team1(teamName, score);
firstTeam.set_team2(teamName2, score2);
Visual studio comes up and says "error: no instance of overloaded function "team::set_team1" matches the argument list".
What am I missing?
This is the exact error I get:
1>c:\users\lab8.cpp(31): error C2664: 'void teams::set_team1(std::string,int)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::string'
1> with
1> [
1> _Ty=std::string
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\lab8.cpp(32): error C2664: 'void teams::set_team2(std::string,int)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::string'
1> with
1> [
1> _Ty=std::string
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> Generating Code...
1>
1>Build FAILED.
error C2664: 'void teams::set_team1(std::string,int)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::string'
From the error message, it is clear that first parameter isn't of type std::string. It is actually a std::vector. So,
firstTeam.set_team1(teamName, score); // Check what is the type of teamName
If you can see that teamName is actually a std::string, then check whether you are compiling the right file. Save the file and try again because the code you posted and the error message has no relation.
Compiler don't provide default constructor( constructor with no arguments ) in case your class overloads the constructor.
class teams
{
string teamName;
string teamName2;
int score;
int score2;
// ...
public:
teams( string t, int s ) : teamName(x), score(s)
// Initializer list
{}
};
But the I don't understand, why you have teamName2, score2 members as members of teams. What if there are 10 teams? Just have an instance for each team and compare them with other instances of teams.
You have declared the two methods without parameters. Convert:
void set_team1();
void set_team2();
into:
void set_team1(string, int);
void set_team2(string, int);
Related
I have a vector array for classes and im making a method that can delete the classes from that array, using std::remove(). The problem is Weapon (the class) doesn't have an operator == that std::remove needs to compare the elements.
the code:
class Weapon {
public:
std::string name;
Weapon(std::string x) {
name = x;
}
Weapon() {}
};
std::vector<Weapon> inventory{};
void removeInventory(Weapon x) {
if (inventorySize != 0) {
auto deleteFromInc = inventory.erase(std::remove(std::begin(inventory), std::end(inventory), x), std::end(inventory));
if (deleteFromInc != std::end(inventory)) {
std::cout << "Item didn't delete\n";
} else {
if (heldWeapon.name == x.name) {
heldWeapon = Weapon();
}
inventorySize = inventory.size();
std::cout << "Item deleted\n";
}
} else {
std::cout << "You have no items\n";
}
}
the error:
Rebuild started...
1>------ Rebuild All started: Project: rpag, Configuration: Debug Win32 ------
1>rpag.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xmemory(1945,1): error C2678: binary '==': no operator found which takes a left-hand operand of type 'Weapon' (or there is no acceptable conversion)
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\guiddef.h(192,15): message : could be 'bool operator ==(const GUID &,const GUID &)' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xmemory(1945,1): message : while trying to match the argument list '(Weapon, const _Ty)'
1> with
1> [
1> _Ty=Weapon
1> ]
1>C:\Users\alber\source\repos\rpag\rpag\rpag.cpp(89): message : see reference to function template instantiation '_FwdIt std::remove<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,Weapon>(_FwdIt,const _FwdIt,const _Ty &)' being compiled
1> with
1> [
1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Weapon>>>,
1> _Ty=Weapon
1> ]
1>Done building project "rpag.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
This is my first question! I hope everything is clear.
std::remove needs operator== (this is requirement of c++ standart library). And you can adds the operator into your class, or use std::remove_if - it use predicate - external function for comparison.
There is third way: you can erase items from vector manually: for (...) { erase ...}
This is a Game Engine for SFML builded in c++. I get some errors that i don't know how to fix it. If someone can solve this problem i will apriciated a lot.
I'm still learning c so por someone could same an obious problem or solucion but i just copied the code from another page and I do exactly the same and mine code isn't working
Errors:
Error C2065: 'StateSystem' : undeclared identifier
Error C2923: 'std::unique_ptr' : 'StateSystem' is not a valid template type
argument for parameter '_Ty'
Error C3203: 'unique_ptr' : unspecialized class template can't be used as a
template argument for template parameter '_Ty', expected a real type
Error C2512: 'std::unique_ptr' : no appropriate default constructor
available
Error C2780: '_OutTy *std::move(_InIt,_InIt,_OutTy (&)[_OutSize])' : expects
3 arguments - 1 provided
1> c:\program files (x86)\microsoft visual studio
12.0\vc\include\xutility(2510) : see declaration of 'std::move'
Error C2893: Failed to specialize function template
'remove_reference<_Ty>::type &&std::move(_Ty &&) throw()'
1> With the following template arguments:
1> '_Ty=Victor::StateRef &'
Error C2227: left of '->Resume' must point to class/struct/union/generic
type
1> type is 'int'
Error C2780: '_OutTy *std::move(_InIt,_InIt,_OutTy (&)[_OutSize])' : expects
3 arguments - 1 provided
1> c:\program files (x86)\microsoft visual studio
12.0\vc\include\xutility(2510) : see declaration of 'std::move'
Error C2893:
Failed to specialize function template 'remove_reference<_Ty>::type
&&std::move(_Ty &&) throw()'
1> With the following template arguments:
1> '_Ty=Victor::StateRef &'
Error C2227: left of '->Initialize' must point to class/struct/union/generic
type
1> type is 'int'
Error C2440: 'return' : cannot convert from 'int' to 'Victor::StateRef &'
And This is the code that provides errors.
State.h
#pragma once
class State
{
public:
virtual void Initialize() = 0;
virtual void HandleInput() = 0;
virtual void Update() = 0;
virtual void Draw(float DeltaTime) = 0;
virtual void Pause()
{
}
virtual void Resume()
{
}
};
StateSystem.h
#pragma once
#include <memory>
#include <stack>
#include "State.h"
typedef std::unique_ptr <StateSystem> StateRef;
class StateSystem
{
public:
StateSystem()
{
}
~StateSystem()
{
}
void AddState(StateRef newStat, bool isReplacing = true);
void RemoveState();
void ProcessStateChanges();
StateRef &GetActiveState();
private:
std::stack<StateRef> _states;
StateRef _newState;
bool _isRemoving;
bool _isAdding;
bool _isReplacing;
};
StateSystem.cpp
#include "StateSystem.h"
void StateSystem::AddState(StateRef newState, bool isRepalcing)
{
this->_isAdding = true;
this->_isReplacing = isRepalcing;
this->_newState = std::move(newState);
}
void StateSystem::RemoveState()
{
this->_isRemoving = true;
}
void StateSystem::ProcessStateChanges()
{
if (this->_isRemoving && !this->_states.empty())
{
this->_states.pop();
if (!this->_states.empty())
{
this->_states.top()->Resume();
}
this->_isRemoving = false;
}
if (this->_isAdding)
{
if (!this->_states.empty())
{
if (this->_isReplacing)
{
this->_states.pop();
}
else
{
this->_states.top()->Pause();
}
}
this->_states.push(std::move(this->_newState));
this->_states.top()->Initialize();
this->_isAdding = false;
}
}
StateRef &StateSystem::GetActiveState()
{
return this->_states.top();
}
there's no StateSystem before typedef std::unique_ptr <StateSystem> StateRef; just add class StateSystem before it.
it says it cannot find the StateSystem class. you have to declare it first like this:
class StateSystem;
typedef std::unique_ptr <StateSystem> StateRef;
class StateSystem
{
//members
};
or put your typedef after the StateSystem definition like this:
class StateSystem
{
//members
};
typedef std::unique_ptr <StateSystem> StateRef;
I have those functions in some open source lib called uWebSockets
any way I try to run its simple example using Visual Studio 2013.
The function I try to call looks like this:
template <bool isServer>
void Group<isServer>::onMessage(std::function<void (WebSocket<isServer>, char *, size_t, OpCode)> handler) {
messageHandler = handler;
}
and the example looks like this:
int main()
{
uWS::Hub h;
h.onMessage([](uWS::WebSocket<uWS::SERVER> ws, char *message, size_t length, uWS::OpCode opCode) {
ws.send(message, length, opCode);
});
h.listen(3000);
h.run();
}
The error I'm getting looks like this :
1>D:\dev\cpp\server\uWebSockets\git\uWebSockets\uWebSockets\examples\echo.cpp(9): error C2668: 'uWS::Group<false>::onMessage' : ambiguous call to overloaded function
1> d:\dev\cpp\server\uwebsockets\git\uwebsockets\uwebsockets\src\Group.h(69): could be 'void uWS::Group<false>::onMessage(std::function<void (uWS::WebSocket<false>,char *,size_t,uWS::OpCode)>)'
1> d:\dev\cpp\server\uwebsockets\git\uwebsockets\uwebsockets\src\Group.h(69): or 'void uWS::Group<true>::onMessage(std::function<void (uWS::WebSocket<true>,char *,size_t,uWS::OpCode)>)'
1> while trying to match the argument list '(main::<lambda_f789fc974d6d87c4a2444e6ded66c2a0>)'
Question:
Can I over come the error without upgrading the Visual Studio to 2015?
or it is not related to the compiler version?
UPDATE :
if i set it to : h.onMessage<true> or h.onMessage<uWS::SERVER>
im getting :
This error :
1>D:\dev\cpp\server\uWebSockets\git\uWebSockets\uWebSockets\examples\echo.cpp(10): error C2678: binary '>' : no operator found which takes a left-hand operand of type 'bool' (or there is no acceptable conversion)
1> could be 'built-in C++ operator>(void (__cdecl *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode), void (__cdecl *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode))'
1> or 'built-in C++ operator>(void (__stdcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode), void (__stdcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode))'
1> or 'built-in C++ operator>(void (__fastcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode), void (__fastcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode))'
1> or 'built-in C++ operator>(void (__vectorcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode), void (__vectorcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode))'
1> while trying to match the argument list '(bool, main::<lambda_f789fc974d6d87c4a2444e6ded66c2a0>)'
I got the same errors, after minor fix I got it running:
int main()
{ uWS::Hub h;
h.onMessage([](uWS::WebSocket<uWS::SERVER>* ws, char *message, size_t length, uWS::OpCode opCode) {
ws->send(message, length, opCode);
});
h.listen(3000);
h.run();
}
I am trying to call a const overload function
void process(std::map<std::string,std::shared_ptr<const Data_Struct>>);
with data I generate. Because I generate the data, I use a non-const version
std::map<std::string,std::shared_ptr<Data_Struct>> my_data;
When I try to call my function with
process(my_data);
I get the error:
error C2664: 'void process(std::map<std::string,std::shared_ptr<const Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>)' : cannot convert argument 1 from 'std::map<std::string,std::shared_ptr<Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'std::map<std::string,std::shared_ptr<const Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
I have tried a variety of casting variations but all fail to do the job. Any help is appreciated.
Here is my test code with the casting variants and associated error codes (I am using Visual C++ community edition 2013):
struct Data_Struct
{
int a;
std::string b;
};
void process(std::map<std::string,std::shared_ptr<const Data_Struct>>);
void calling_function() {
std::map<std::string,std::shared_ptr<Data_Struct>> my_data;
my_data.emplace("test",std::shared_ptr<Data_Struct>(new Data_Struct)).first->second->a = 2;
process(my_data);
// error C2664: 'void process(std::map<std::string,std::shared_ptr<const Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>)' : cannot convert argument 1 from 'std::map<std::string,std::shared_ptr<Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'std::map<std::string,std::shared_ptr<const Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
process(std::map<std::string,std::shared_ptr<const Data_Struct>>(my_data));
// error C2440: '<function-style-cast>' : cannot convert from 'std::map<std::string,std::shared_ptr<Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'std::map<std::string,std::shared_ptr<const Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
process((std::map<std::string,std::shared_ptr<const Data_Struct>>)my_data);
// error C2440: 'type cast' : cannot convert from 'std::map<std::string,std::shared_ptr<Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'std::map<std::string,std::shared_ptr<const Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
process(static_cast<std::map<std::string,std::shared_ptr<const Data_Struct>>>(my_data));
// error C2440: 'static_cast' : cannot convert from 'std::map<std::string,std::shared_ptr<Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'std::map<std::string,std::shared_ptr<const Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
process(reinterpret_cast<std::map<std::string,std::shared_ptr<const Data_Struct>>>(my_data));
// error C2440: 'reinterpret_cast' : cannot convert from 'std::map<std::string,std::shared_ptr<Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'std::map<std::string,std::shared_ptr<const Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
process(const_cast<std::map<std::string,std::shared_ptr<const Data_Struct>>>(my_data));
// error C2440: 'const_cast' : cannot convert from 'std::map<std::string,std::shared_ptr<Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'std::map<std::string,std::shared_ptr<const Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
process(std::static_pointer_cast<std::map<std::string,std::shared_ptr<const Data_Struct>>>(my_data));
// error C2784: 'std::shared_ptr<_Ty> std::static_pointer_cast(const std::shared_ptr<_Ty2> &) throw()' : could not deduce template argument for 'const std::shared_ptr<_Ty2> &' from 'std::map<std::string,std::shared_ptr<Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
process(std::const_pointer_cast<std::map<std::string,std::shared_ptr<const Data_Struct>>>(my_data));
// error C2784: 'std::shared_ptr<_Ty> std::const_pointer_cast(const std::shared_ptr<_Ty2> &) throw()' : could not deduce template argument for 'const std::shared_ptr<_Ty2> &' from 'std::map<std::string,std::shared_ptr<Data_Struct>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
}
void process(std::map<std::string,std::shared_ptr<const Data_Struct>> data) {
for(auto &d : data)
std::cout << d.first << std::endl;
}
The two map types are totally unrelated and in spite of the apparent similiarity cannot be used interchangeably. You do have a few options though:
Populate your data class in its constructor so you don't have to mutate it after creation.
Have the process function work on a templated iterator range rather than a specific container.
Have the process function work on a templated container rather than a specific type.
The function process() takes its parameter by value. This means that when it's called a copy of the map is taken, processed and then discarded. I don't know if that's your intention. Assuming it is, what you need is a conversion function:
#include <iostream>
#include <string>
#include <map>
#include <memory>
struct Data_Struct
{
int a;
std::string b;
};
void process(std::map<std::string,std::shared_ptr<const Data_Struct>>);
std::map<std::string,std::shared_ptr<const Data_Struct>>
convert_map(const std::map<std::string,std::shared_ptr<Data_Struct>>& source)
{
std::map<std::string,std::shared_ptr<const Data_Struct>> ret;
for(const auto& item : source) {
ret.emplace(item.first, item.second);
}
return ret;
}
void process(std::map<std::string,std::shared_ptr<const Data_Struct>> data) {
for(auto &d : data)
std::cout << d.first << std::endl;
}
void calling_function() {
std::map<std::string,std::shared_ptr<Data_Struct>> my_data;
my_data.emplace("test",std::shared_ptr<Data_Struct>(new Data_Struct)).first->second->a = 2;
process(convert_map(my_data));
}
using namespace std;
int main()
{
calling_function();
return 0;
}
Does anyone know why this will not compile? I've tried both VS 2008 and GCC 4.something and both spit out errors. It doesn't matter whether or not I'm referencing "ThisFunctionDoesNotCompile()".
I can workaround this by just passing 'InternalType' as a second template parameter to Base, but I'm still curious why this comes up as an error.
#include <iostream>
using namespace std;
class DataClass
{
public:
int m_data;
};
template<typename DerivedType>
class Base
{
public:
int ThisFunctionCompiles()
{
// No problems here.
typename DerivedType::InternalType temp;
temp.m_data = 5;
return temp.m_data;
}
// error C2039: 'InternalType' : is not a member of 'Derived<InInternalType>'
typename DerivedType::InternalType ThisFunctionDoesNotCompile()
{
return static_cast<DerivedType*>(this)->GetInternalData();
}
};
template<typename InInternalType>
class Derived : public Base<Derived<InInternalType> >
{
public:
typedef InInternalType InternalType;
InternalType GetInternalData()
{
return m_internalData;
}
private:
InternalType m_internalData;
public:
void SetInternalData( int newVal )
{
m_internalData.m_data = newVal;
}
};
int main()
{
Derived<DataClass> testDerived;
testDerived.SetInternalData( 3 );
cout << testDerived.GetInternalData().m_data << endl;
cout << testDerived.ThisFunctionCompiles() << endl;
// The compiler gives an error regardless of whether or not this is commented out.
//cout << testDerived.ThisFunctionDoesNotCompile().m_data << endl;
return 0;
}
These are the errors I get in VS 2008:
1>e:\test\generaltestprogram\generaltestprogram\main.cpp(27) : error C2039: 'InternalType' : is not a member of 'Derived<InInternalType>'
1> with
1> [
1> InInternalType=DataClass
1> ]
1> e:\test\generaltestprogram\generaltestprogram\main.cpp(35) : see reference to class template instantiation 'Base<DerivedType>' being compiled
1> with
1> [
1> DerivedType=Derived<DataClass>
1> ]
1> e:\test\generaltestprogram\generaltestprogram\main.cpp(58) : see reference to class template instantiation 'Derived<InInternalType>' being compiled
1> with
1> [
1> InInternalType=DataClass
1> ]
1>e:\test\generaltestprogram\generaltestprogram\main.cpp(27) : error C2146: syntax error : missing ';' before identifier 'ThisFunctionDoesNotCompile'
1>e:\test\generaltestprogram\generaltestprogram\main.cpp(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\test\generaltestprogram\generaltestprogram\main.cpp(28) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\test\generaltestprogram\generaltestprogram\main.cpp(28) : warning C4183: 'ThisFunctionDoesNotCompile': missing return type; assumed to be a member function returning 'int'
And these are what GCC gives me:
main.cpp: In instantiation of 'Base<Derived<DataClass> >':
main.cpp:96: instantiated from 'Derived<DataClass>'
main.cpp:119: instantiated from here
main.cpp:88: error: no type named 'InternalType' in 'class Derived<DataClass>'
At the time that the templated class Base is instantiated as a parent of the class Derived, the class Derived is not a complete type.
Since Base<Derived<DataClass> > is a parent class of Derived<DataClass>, it must be instantiated before Derived<DataClass> can be instantiated. So when the class Base<Derived<DataClass> > is built from the template, Derived<DataClass> behaves as if it were a forward declaration. And as you're probably aware, you can't reference members of incomplete types, nor can your forward-declare nested types, so you're out of luck here.
This, by the way, is why it's difficult to implement a properly covariant clone() method using templates. See here and here (mine).