I see a code using exactly this, but that code works and mine not, any idea why?
PD: im trying to implement this commit. See that the code is exactly the same
for(const auto& tx : block.vtx)
if (txHash == tx->GetHash()) {
txNew = tx;
foundAtOut = *pindex;
return true;
}
main.cpp:2471:25: error: no match for ‘operator=’ (operand types are ‘CTransactionRef’ {aka ‘std::shared_ptr<const CTransaction>’} and ‘const CTransaction’)
txNew = tx;
Read the error message carefully: you are trying to assign an object of type const CTransaction to a shared pointer of type std::shared_ptr<const CTransaction>. But you can't do that using operator=, because its argument should be a shared_ptr or unique_ptr, as described at cppreference.
Depending on your actual code, I think, you can create a new shared_ptr for the const CTransaction object and then assign to it.
Related
So I have this structure available and I have to make the output of the same format of the structure. However, whatever I have tried has not working and online search hasn't netted any results. I am getting error in the function that its->SeekToLast() can't be assigned
kvs.hh
`class KVS {
public:
struct Sentinel {};`
kvs.cc
auto KVS::end() const -> KVS::Sentinel {
rocksdb::Iterator* its = db->NewIterator(rocksdb::ReadOptions());
KVS::Sentinel sent;
sent->its->SeekToLast();
return sent;
}
In member function ‘cloudlab::KVS::Sentinel cloudlab::KVS::end() const’:
error: no match for ‘operator=’ (operand types are ‘cloudlab::KVS::Sentinel’ and ‘void’)
72 | sent = its->SeekToLast();
expecting to return the required value properly
Seek to last doesn't return anything. You can't assign it to a variable.
You should seek to last and then return the iterator
When I am calling SetParams function from the below function, it is throwing an error "cannot convert argument 1 from 'std::wstring' to 'wchar_t *'"
Can anyone please help me on this?
int main()
{
IVerify* pReader = new BCReader();
std::wstring oemPathKey;
pReader->SetParams(oemPathKey, L"read");
delete pReader;
return 0;
}
void BCReader::SetParams(wchar_t* wszParams, wchar_t* wszParamType)
{
m_wszParamType = wszParamType;
m_wszParams = wszParams;
}
The member variables are declared like as shown below:
class IVerify
{
private:
wchar_t* m_wszParams;
wchar_t* m_wszParamType;
};
There are two parts of the right answer:
1. You need to use pReader->SetParams(oemPathKey.c_str(), L"read");
2. Your approach is not safe, you trying to keep pointer to string in the class members.
But if the original string will go out of scope then you will receive Access Vioalation, if you are lucky :). So in SetParams you need to copy source string to the class members uisng for example wscpy (I recommend something like wscpy_s), also you need correctly handle allocation/deallocation for string copy.
in header.h:
before class: class Treadmill;
private: Treadmill* treadmillList;
public: bool addTreadmill(Treadmill *Obj);
in header.cpp:
Constructor: treadmillList = new Treadmill[LISTSIZE];
bool Trainee::addTreadmill(Treadmill *Obj) {
treadmillList[numOfTreadmills++]=Obj;
}
Result of compiling:
treadmill.cpp: In member function ‘bool Trainee::addTreadmill(Treadmill*)’:
treadmill.cpp:39:34: error: no match for ‘operator=’ (operand types are ‘Treadmill’ and ‘Treadmill*’)
treadmillList[numOfTreadmills++]=Obj;
^
treadmill.cpp:39:34: note: candidate is:
In file included from treadmill.cpp:3:0:
treadmill.h:3:7: note: Treadmill& Treadmill::operator=(const Treadmill&)
class Treadmill {
^
treadmill.h:3:7: note: no known conversion for argument 1 from ‘Treadmill*’ to ‘const Treadmill&’
From just looking at the code you posted I think your trying to store Treadmill pointers, or addresses to Treadmill objects in an array of type Treadmill. If you want to store pointers to type Treadmill in an array try:
Treadmill** treadmillList;
Constructor: treadmillList = new Treadmill*[LISTSIZE];
This is just based on what I observed though you didnt state what you were aiming for and what your problem was exactly.
please try this code
bool Trainee::addTreadmill(Treadmill *Obj) {
treadmillList[numOfTreadmills++] = *Obj;
}
I'm currently writing some code for a game and part of that involves creating a history of the actions that have taken place so far in the game. This history is stored in a vector of state_pair_t's pairs of actions (action_t's) and pointers to the result gamestate after an action has been made. Now I have a some function that looks through the history starting at the most recent point in time and iterates backwards until an action of a certain type is found then returns a reference to that. Now we decided that it might be a good design move to use boost optional to return a no_action if no action was found and use boost::optional to deal with these functions which should return a value but which might not have a value to return. When I've actually tried to implement this I run into an error that I don't understand:
typedef boost::variant<
A,
B,
B
> action_t;
typedef boost::optional< action_t& > opt_action_ref_t;
const opt_action_ref_t no_action = opt_action_ref_t();
/*! A state pair is the combination of a particular action and the resulting game state */
typedef std::pair< const action_t, game_state_ptr > state_pair_t;
opt_action_ref_t get_last_non_A_action() const{
std::vector< state_pair_t >::const_reverse_iterator rcit;
for(rcit = m_states.rbegin(); m_states.rend() != rcit ; ++rcit){
if(!(is_action_type< A >(rcit->first))){
return rcit->first; \\error at compile time
}
}
return no_action;
}
Now this gives a compile error:
Error error C2664: 'boost::optional<T>::optional(boost::none_t)' : cannot convert parameter 1 from 'const action_t' to 'boost::none_t'
Now if I change this slightly to:
if(!(is_action_type< A >(rcit->first))){
return boost::optional<action_t>(rcit->first);
}
I get another error:
error C2664: 'boost::optional<T>::optional(boost::none_t)' : cannot convert parameter 1 from 'boost::optional<T>' to 'boost::none_t'
I'm not sure what either of these errors are trying to tell me here. Is what I'm trying to do here not a good a good idea with boost::optional? Is it even possible?
Optional references are in themselves a good idea; they're specifically mentioned as such in the recent paper n3527, intended for standardisation as a library component in C++14. They're supported in Boost.Optional.
The problem with your code is that you're trying to bind a non-const optional reference to a const lvalue; if you change boost::optional< action_t& > to boost::optional< const action_t& > it should compile fine.
I'm trying to get this C++ method to return an array of b2Fixture instances. It iterates of a series of JRContact instances, which are defined like:
struct JRContact {
b2Fixture *fixtureA;
b2Fixture *fixtureB;
bool operator==(const JRContact& other) const
{
return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
}
};
n.b. I'm a total stranger to C++, don't hesitate to mention weird things I might have done in that code ;-)
The following fails to compile (XCode compiler on MacOS), see errors in comments:
id AbstractContactListener::getFixturesOfTypeCollidingWithFixture(b2Fixture *fix, int type){
std::vector<b2Fixture> fixtures;
std::vector<JRContact>::iterator ct;
JRContact contact;
for (ct = _contacts.begin(); ct != _contacts.end(); ct++){
contact = *ct;
if (
( (fix == contact.fixtureA) || (fix == contact.fixtureB) ) &&
( contactContainsType(contact, type) )
){
if (fix == contact.fixtureA) {
// error: Semantic Issue: Reference to type 'const value_type' (aka 'const b2Fixture') could not bind to an lvalue of type 'b2Fixture *'
fixtures.push_back(contact.fixtureB);
}
else {
// error: Semantic Issue: Reference to type 'const value_type' (aka 'const b2Fixture') could not bind to an lvalue of type 'b2Fixture *'
fixtures.push_back(contact.fixtureA);
}
}
}
// error: Semantic Issue: No viable conversion from 'std::vector<b2Fixture>' to 'id'
return fixtures;
}
Thanks for your time!
Change :
std::vector<b2Fixture> fixtures;
to :
std::vector<b2Fixture *> fixtures;
About the return type you can change it either to void* or std::vector<b2Fixture *> * and use : return &fixtures;
But pay attention the your vector is local so allocate it for not returning a pointer to an invalid location. (And ofcourse remember to free it when you done using it).
It's not really clear what you want to do, but the problem is you're telling the compiler that AbstractContactListener::getFixturesOfTypeCollidingWithFixture will return an id and you're instead returning an std::vector<b2Fixture>.
From the name of the function, I guess you might want to return a vector, so change the signature to:
std::vector<b2Fixture> AbstractContactListener::getFixturesOfTypeCollidingWithFixture
(b2Fixture *fix, int type)
You're also pushing pointers in your vector when you should be pushing objects:
fixtures.push_back(*(contact.fixtureB));
The vector fixtures holds b2Fixture instances, but the contact.fixtureA is a b2Fixture*.
Either:
dereference it:
fixtures.push_back(*(contact.fixtureA)); // Same for 'fixtureB'.
or,
change the type of fixtures:
std::vector<b2Fixture*> fixtures;
There is also a mismatch between the function return type and what is actually being returned. If you want to return fixtures, have the return type match the type of fixtures.