make_pair(string, class): error: expected primary-expression before â)â token - c++

error: expected primary-expression before â)â token
I'm not entirely sure what's going on here, since my friends also working on this project can't seem to tell what's wrong. Any help on this error would be appreciated. The line that the error is referring to has a comment on it pointing it out. I'm trying to insert a pair into a map by the code below.
theCandidates is a map<string, class>, and in this case, that class is called Candidate.
void TallyVotes::initialize(Scanner& inStream)
{
numberOfLosers = 0;
numberOfVotes = boost::lexical_cast<int>(inStream.next());
numberOfCandidates = boost::lexical_cast<int>(inStream.next());
for(int i = 0; i < numberOfVotes ; i++)
{
for(int j = 0; j < numberOfCandidates ; i++)
{
theVotes[i][j] = inStream.next();
cand = theVotes[i][j];
if(i == 0)
{
theCandidates.insert(make_pair(cand, Candidate));//ERROR ON THIS LINE
}
}
}
} // void TallyVotes::initialize(Scanner& inStream)

The make_pair function takes two values as arguments, not a value and a type.
Try e.g.
make_pair(cand, Candidate())
// Note parentheses ^^
The expression Candidate() create a temporary object, which is then copied into the std::map.

Related

instantiating a class object (template) from a base class

Warning newby
Base class trying to instantiate template class
Rather than put all the code in , I'll try excerpts for brevity
Base class constructor has error: Trying to create an object of template class Queue.
There is a typedef setting data_type = Card (a
common class).
DeckOfCards::DeckOfCards()
{
Queue<data_type>* deck;
deck = new Queue<data_type>;
fill_deck(deck&); **"error: expected primary-expression before ‘)’ token"**
}
**fill_deck declaration:**
from the header : void fill_deck(const Queue<data_type>& data);
void DeckOfCards::fill_deck( const Queue<data_type>& deck)
{
for (int i=0; i < 4 ; i++)
{
for (int j=1; j< 14 ; j++)
{
if ( j == 11 ) {
cardID = "J" + suits[i];
cardValue = 10;
} else
if ( j == 12 ) {
cardID = "Q" + suits[i];
cardValue = 10;
} else
if ( j == 13 ) {
cardID = "K" + suits[i];
cardValue = 10;
} else {
cardID = suits[i] + IntToString(j);
cardValue = j;
}
cardFacing = false;
Card* cardPtr = new Card(cardID,cardValue, cardFacing);
deck.enqueue(cardPtr&); **"error: expected primary-expression before ‘)’ token"**
delete cardPtr;
}
}
cout << "\nFinished add " ;
}
Method enqueue from Template Queue:
template <class data_type>
void Queue<data_type>::enqueue(const data_type& data){
qList->addToTail(data);
length++;
}
The plan was to create a Queue class object and populate that queue
with type Card another class. I couldn't Pass a reference parameter to
the function DeckOfCards::fill_deck() and I couldn't refer to the new
Queue object.
Any help appreciated
fill_deck accepts a reference const Queue<data_type>& so since you created a pointer
Queue<data_type>* deck;
deck = new Queue<data_type>;
you'd dereference it to call fill_deck
fill_deck(*deck);
similar problem later, which should be
deck.enqueue(*cardPtr);
As a general note, you have a lot of new allocations that likely could instead be stack allocated. You're likely leaking memory by doing so, though it is hard to say without seeing the context of the rest of the class design.

Sorting doesn't work with templated class

I have an insertion sort function
void insertionSort(ArrayList<int> myData)
{
for (int i = 1; i < myData.getSize(); i++) {
int index = myData[i];
int j = i;
while (j > 0 && myData[j-1] > index) {
myData.swap(j - 1, j);
j--;
}
myData[j] = index;
}
}
which uses this swap function
template<class TYPE>
void ArrayList<TYPE>::swap(int from, int to) throw(std::out_of_range)
{
int temp = 0;
temp = this->items[from];
this->items[from] = this->items[to];
this->items[to] = temp;
swapNum++;
}
This is how my private methods look like
TYPE * items;
int currentLength;
static int swapNum;
I have an overloaded [] operator and a getSize() function that I think I wrote well and not contributing to my problem. Now if I do this in my main.cpp
ArrayList<int>m_Data(1);
and append say 4,2,9,1 on the m_Data and call
insertionSort(m_Data);
I get two errors
1. Error C2440 '=': cannot convert from 'std::string' to 'int'
on the swap function and
2. The insertion sort doesn't work
First problem: it should be something like TYPE temp = this->items[from]. After repairing it (I used STL swap) function works. Well, it works on STL vector and swap. If you still do have problem, then your array structure is probably invalid.
EDIT: In function 'insertionSort' shouldn't you have template (as in swap function)?

C++ error: How to solve malloc: *** error for object ... pointer being freed was not allocated

Why am I getting this error after I compile:
Program1(49296,0x7fff74b72000) malloc: *** error for object 0x7f9222500040: pointer being freed was not allocated
Is there a way to resolve it? I only get it when I do recursion, but if I take out this line:
if(counter == G.nameSet.size())
{
explore(G, *adjPtr);
}
...It then works
void explore(Graph & G, Node & foo)
{
G.nameSet.insert(foo.name());
set <string> tempNameSet;
list <Node> adjacentList = G.getAdjNodes(foo);
int y = adjacentList.size();
list<Node>::iterator adjPtr = adjacentList.begin();
for(int i=0; i < y; i++ )
{
tempNameSet = G.nameSet;
set<string>::iterator nSetPtr = G.nameSet.begin();
int counter = 0;
for (int j = 0; j < G.nameSet.size(); j++)
{
if(*nSetPtr != adjPtr->name())
counter++;
if(tempNameSet.size() > 1)
nSetPtr = tempNameSet.erase(nSetPtr);
}
if(counter == G.nameSet.size())
{
explore(G, *adjPtr); //even when I plug in foo instead of *adjPtr, i get the same error
}
if(adjacentList.size() > 1)
adjPtr = adjacentList.erase(adjPtr);
}
}
It's hard because that function isn't calling free directly. At a guess, it's crashing in erase(adjPtr). To verify that is the case, put a diagnostic printf before and after the call, and the crash should happen between the calls. Probably adjPtr is going empty and that causes erase to fail, but exactly where the logic error is is quite hard to tell.

push_back vector of pointers of pointers

std::vector<BPatch_point *> *points;
points = functions[0]->findPoint(BPatch_entry);
if ((*points).size() == 0)
{
cout << "Failed to get the points" << endl;
}
std::vector<ldframework::Point *> *new_points;
for(unsigned int i = 0; i < points->size(); i++ )
{
ldframework::Point *pt1 = new ldframework::PointImpl((*points[0]));
new_points->push_back(pt1);
}
The PointImpl constructor is :
ldframework::PointImpl::PointImpl(const BPatch_point&* po)
{
Bpatch_point *_bpoint=new BPatch_point(*po);
}
WHile compiling im getting following error. Could you please help
>BfunctionImpl.cpp: In member function âvirtual const std::vector<ldframework::Point*, std::allocator<ldframework::Point*> >* ldframework::BfunctionImpl::find_Point(ldframework::locClass)â:
BfunctionImpl.cpp:86: error: expected type-specifier
BfunctionImpl.cpp:86: error: cannot convert âint*â to âldframework::Point*â in initialization
BfunctionImpl.cpp:86: error: expected â,â or â;â
THe 86 line number code is
ldframework::Point *pt1 = new ldframework::PointImpl((*points[0]));
Have a look at the (*points[0]) part of line 86.
Since points is a pointer to a vector and the array subscript operator [] has a higher precedence than the unary indirection operator *, this is parsed as *(*(points+0)).
You might try to replace this by (*points)[0] or maybe even: (*points)[i]
Furthermore, new_points is a pointer to a vector, that has not been assigned to a pointer to a valid object before it is used.
Additionally, I see that you use a lot of pointers, which causes extra indirections, decrease of locality, and gives you the burden of cleaning-up things or causing memory leaks. Does new_points have to be a vector of pointers to ldframework::Point? You could use a vector of std::unique_ptr<ldframework::Point> to avoid the clean-up burden, but you can also consider:
std::vector<ldframework::PointImpl> *new_points =
new std::vector<ldframework::PointImpl>;
for(unsigned int i = 0; i < points->size(); i++ )
{
new_points->push_back( ldframework::PointImpl((*points)[i]) );
}
or even better:
std::vector<ldframework::PointImpl> new_points;
for(unsigned int i = 0; i < points->size(); i++ )
{
new_points.push_back( ldframework::PointImpl((*points)[i]) );
}

error: ‘class std::vector<route, std::allocator<route> >’ has no member named ‘exitPoint’

Please tell me if some more info is needed here:
Global declarations:
typedef struct route
{
int exitPoint;
bool allBranchesTraversed;
} route;
****vector <route> routeVector;****
The culprit func is getting called from:
int main ()
{
....
do
{
****currentExitPoint = returnShortestWeightedBranch (&routeVector);****
if (currentExitPoint != -1)
{
objRoute.exitPoint = currentExitPoint;
routeVector.push_back (objRoute);
}
else
{
break;
}
} while (1);
}
The error is in this func on the line with **:
int returnShortestWeightedBranch (vector <route> *objRouteVector)
{
....
for (unsigned int h = 0; h < objRouteVector->size (); h++)
{
// Locate 'currentExitPoint' in the vector 'exitPointDetailsVector'.
for (i = 0; i < exitPointDetailsVector.size(); i++)
{
// If located
****if (objRouteVector[h].exitPoint == exitPointDetailsVector[i].exitPoint)****
{
// For all the branches of the 'currentExitPoint',
for (j = 0; j < exitPointDetailsVector[i].branchesVector.size(); j++)
{
...............
}
If you use vector <route> *objRouteVector as parameter, you need (*objRouteVector)[h].exitPoint. Better is using reference: vector <route> &objRouteVector.
You took a pointer to objRouteVector, you need to take a reference. Your code indexing objRouteVector isn't indexing the vector at all- it's indexing the pointer.