expected primary-expression before ?.? token
Author |
Message |
a22asin
|
Posted: Wed Nov 19, 2014 4:41 pm Post subject: expected primary-expression before ?.? token |
|
|
So i am trying to call my member function which is static from my main function. However, i get the error `expected primary-expression before "." token`. What does this error mean in terms of my code? and how could i fix it? it shows up both times when i call `rollTwoDie(Die d1,Die d2)` by `Die.rollTwoDie(regDieBag[i], loadedDieBag[i]);` and im not sure why im getting this error. Oh and im not sure if this helps or not, but loadedDieBag contains the derived class object LoadedDie which is derived from the base class Die below.
My part of my Die.cpp
code: | #include<iostream>
#include <cstdlib>
using namespace std;
class Die{
public:
Die();
Die(int numSide);
virtual int roll() const;
static int rollTwoDice(Die d1, Die d2);
int side;
};
int Die::rollTwoDice(Die d1, Die d2){
return d1.roll()+d2.roll();
} |
My Tester class:
code: | #include "LoadedDie.cpp"
#include<iostream>
#include<time.h>
#include <stdlib.h>
using namespace std;
int main(){
Die *regDieBag[10];
cout << "Rolling dice from regular dice bag..."<<endl;
cout << "Dice[";
for (int i = 0; i < 10; i++){
regDieBag[i] = new Die(SIZE);
if (i == 9){
cout << "Die" << (i+1) << ": " << regDieBag[i]->roll() << "]" <<endl;
}else{
cout << "Die" << (i+1) << ": " << regDieBag[i]->roll() << ", ";
}
}
LoadedDie *loadedDieBag[10];
cout << endl << "Rolling dice from loaded dice bag..."<<endl;
cout << "LoadedDice[";
for (int i = 0; i < 10; i++){
loadedDieBag[i] = new LoadedDie(SIZE);
if (i == 9){
cout << "Die" << (i+1) << ": " << loadedDieBag[i]->roll() << "]" <<endl;
}else{
cout << "Die" << (i+1) << ": " << loadedDieBag[i]->roll() << ", ";
}
}
cout << endl << "Rolling regular and loaded dice again and finding sum of both dice..."<<endl;
cout << "SUM [";
for (int i = 0; i < 10; i++){
loadedDieBag[i] = new LoadedDie(SIZE);
if (i == 9){
//error occurs at the line below when i do Die.rollTwo...
cout << "Die" << (i+1) << ": " << Die.rollTwoDie(regDieBag[i], loadedDieBag[i]) << "]" <<endl;
}else{
cout << "Die" << (i+1) << ": " << Die.rollTwoDie(regDieBag[i], loadedDieBag[i]) << ", ";
}
}
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Thu Nov 20, 2014 3:55 pm Post subject: RE:expected primary-expression before ?.? token |
|
|
Instead of:
code: | Die.rollTwoDie(...) |
you probably want:
code: | Die::rollTwoDie(...) |
which is the correct syntax for a static method call in C++.
Fixing that problem will probably not make your program build. The type of regDieBag[i] is Die*, but Die::rollTwoDie expects Die. |
|
|
|
|
|
|
|