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