compare_exchange_weak() compile error? - c++

My code looks like this:
void C::addB(std::atomic<B>& b)
{
B* b2 = b.load();
B newValue = B();
bool result = b.compare_exchange_weak(b2, newValue, std::memory_order_relaxed, std::memory_order_release);
}
and the compiler keeps complaining that the signature does not match a three-member overloaded form of compare_exchaneg_weak:
note: candidate expects 3 arguments, 4 provided

Your code gives me rather more error messages than the fragment you've posted. The most relevant are
error: cannot convert ‘B’ to ‘B*’ in initialisation
note: no known conversion for argument 1 from ‘B*’ to ‘B&’
indicating that you're declaring a pointer when you want an object:
B b2 = b.load();

http://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange
Refer the prototypes from above URL and pass parameters accordingly.

Related

no match for ‘operator=’ when std::shared_ptr

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.

no known conversion for argument 1 from ‘Graph<int>*’ to ‘Graph<int>&’

I can't understand the problem I am facing here :
class Dijkstra {
public:
Dijkstra(Graph<T> &graph, bool verbose = false)
:m_graph(graph), m_verbose(verbose){ }
[ .. ]
}
Graph<int> *custom = Graph<int>::custom((int *) &nodes[0], 4, 5);
Dijkstra<int> spt(custom, true);
Isn't the Dijkstra constructor taking a reference, and if so, why on earth is the compiler complaining ?
graph.cpp:222:37: error: no matching function for call to ‘Dijkstra<int>::Dijkstra(Graph<int>*&, bool)’
Dijkstra<int> spt(custom, true);
^
graph.cpp:222:37: note: candidates are:
graph.cpp:128:3: note: Dijkstra<T>::Dijkstra(Graph<T>&, bool) [with T = int]
Dijkstra(Graph<T> &graph, bool verbose = false)
^
graph.cpp:128:3: note: no known conversion for argument 1 from ‘Graph<int>*’ to ‘Graph<int>&’
graph.cpp:126:7: note: Dijkstra::Dijkstra(const Dijkstra&)
class Dijkstra {
I have the feeling I am getting it wrong, all of it.
A pointer and a reference are 2 different things, and in a strong typed language, aren't always compatible. You should look at the doc for more information. Anyway, here is a solution for your case :
Graph<int> *custom = Graph<int>::custom((int *) &nodes[0], 4, 5);
Dijkstra<int> spt(&custom, true);
Adding & in front of a ref return the address of the object, and so is a pointer.

error: no matching function for call

I'm calling a one argument constructor and I'm getting an error that seems to read that I'm calling a no argument constructor (which doesn't and shouldn't exist).
This is the error I'm getting
g++ -g -c predictor.C
In file included from predictor.C:5:
PHT.C: In constructor 'PHT::PHT(int)':
PHT.C:5: error: no matching function for call to'TwoBitPredictorTable::TwoBitPredictorTable()'
TwoBitPredictorTable.C:5: note: candidates are: TwoBitPredictorTable::TwoBitPredictorTable(int)
predictor.h:25: note: TwoBitPredictorTable::TwoBitPredictorTable(const TwoBitPredictorTable&)
Here is the constructor call on line 5 in PHT.C
PHT::PHT(int rows)
{
predictor = TwoBitPredictorTable(rows);
}
The class definition for PHT is:
class PHT
{
TwoBitPredictorTable predictor;
public:
PHT(int rows);
bool update(unsigned int pc, unsigned int ghr, bool outcome);
bool getPrediction(unsigned int pc, unsigned int ghr);
};
The idea is to make a class PHT which wraps a TwoBitPredictorTable.
I'm pretty new to C++, but after hours of searching for an answer, I'm asking for your help. Thanks in advance :)
You need to call the constructor in the initialization list. What you have now is equivalent to:
PHT::PHT(int rows) :
predictor() // <-- error, no default constructor
{
predictor = TwoBitPredictorTable(rows);
}
Instead:
PHT::PHT(int rows) :
predictor(rows)
{
}
Looks like TwoBitPredictorTable has no default constructor. You should use the initializer list to construct TwoBitPredictorTable during PHT construction.
PHT::PHT(int rows) : predictor(rows)
{
}
Should look something like this.

Adding objects to array in C++

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;
}

conversion from 'bool' to non-scalar type 'ManetAddress' requested

I have this error "conversion from 'bool' to non-scalar type 'ManetAddress' requested" I only know it has something to do with the .H file but i do not know what is the error. ANybody mind helping? thanks a lot.
Class file:
bool ManetRoutingBase::omnet_exist_rte(struct in_addr dst)
{
//ManetAddress add = omnet_exist_rte(dst.s_addr);
ManetAddress add = omnet_exist_rte(dst);
if (add.isUnspecified()) return false;
else if (add.getIPv4() == IPv4Address::ALLONES_ADDRESS) return false;
else return true;
}
RoutingBase.H file
virtual ManetAddress omnet_exist_rte(ManetAddress dst); //FIXME revise return values
virtual bool omnet_exist_rte (in_addr dst); //FIXME remove it, use the another version
Because in this statement:
ManetAddress add = omnet_exist_rte(dst);
Here you call the function recursively and assign the boolean result to something else.
You have two overloads of the function omnet_exist_rte. The compiler knows which one to call by looking at the argument you use. If you use struct in_addr it will call the function that takes an struct in_addr argument (and which returns bool).
To call the function which return a ManetAddress, you have to call it with an argument of type ManetAddress.
As you have now, since you use the same argument type, it will call the same function recursively (over and over again, leading to a stack overflow). However, since you try to assign the bool return value to a ManetAddress the compiler gives an error (since ManetAddress and bool are not compatible).
What you must do is create a variable of type ManetAddress, and use that variable to call the function:
ManetAddress new_dst;
// TODO: Convert from `struct in_addr` to `ManetAddress`...
ManetAddress add = omnet_exist_rte(new_dst);