Cannot dereference double pointer, " no match for operator* " - c++

I'm trying to search through an array of pointers to objects of class Shape. I have written the following code. However, I'm getting this error: "no match for operator*", and I don't know where to go from here. Any help is appreciated.
Shape** shapesArray;
bool doesNameExist(string name) {
for (int i = 0; i < shapeCount; i++)
{
if(*(shapesArray[i])->getName() == name)
{
return true;
}
else
{
return false;
}
}
}

shapesArray is a Shape**
shapesArray[i] is Shape*
(shapesArray[i])->getName() is dereferencing shapesArray[i] and calls its
member getName
So far nothing wrong. I guess this is what you actually want to get, but you add another *:
*(shapesArray[i])->getName() tries to dereference what was returned from getName (a std::string perhaps?)
PS: You return from the loop in the first iteration in either case. If you want to search in the array you need to loop until you find it (then return true) or loop till the end (then return false after the loop, because it wasn't found).

Related

C++: Loop will run at most once (loop increment never executed)

I know there is a topic Loop will run at most once (loop increment never executed), but it does not answear my question.
I have following code:
class Station{
public:
Station();
void addWartezimmer(Wartezimmer wartezimmer);
void addBehandlungszimmer(Behandlungszimmer behandlungszimmer);
int getWartezimmer();
private:
list<Wartezimmer> WartezimmerListe;
list<Behandlungszimmer> BehandlungszimmerListe;
};
Station::Station(){
}
void Station::addWartezimmer(Wartezimmer wartezimmer){
this->WartezimmerListe.push_back(wartezimmer);
}
void Station::addBehandlungszimmer(Behandlungszimmer behandlungszimmer){
this->BehandlungszimmerListe.push_back(behandlungszimmer);
}
int Station::getWartezimmer(){
list<Wartezimmer>::iterator i;
for (i = WartezimmerListe.begin(); i != WartezimmerListe.end(); i++){
return i->getAnzahlPlaetze();
}
return 0;
};
int main(int argc, const char * argv[]) {
Zimmer zimmer("05.23");
Wartezimmer WarteZimmer1(12, "01.12");
Wartezimmer WarteZimmer2(14, "03.12");
Behandlungszimmer Behandlungszimmer1("Intensiv", "01.01");
Station West;
West.addWartezimmer(WarteZimmer1);
West.addWartezimmer(WarteZimmer2);
West.addBehandlungszimmer(Behandlungszimmer1);
cout << West.getWartezimmer();
}
If I execute this command, it will be printed "12". I expect "1214", because there are two Objects which have Numbers.
If I change the function to void and print it in the function, it is "1214".
void Station::getWartezimmer(){
list<Wartezimmer>::iterator i;
for (i = WartezimmerListe.begin(); i != WartezimmerListe.end(); i++){
cout << i->getAnzahlPlaetze();
}
};
So my specific Question: How can I return a value of a function, if it is more than one value from a list? I donĀ“t want only "12", I want "1214" because they are 2 Objects.
If you would like to get whole data you could change int getWartezimmer(); to
list<Wartezimmer> Station::getWartezimmer()
{
return WartezimmerListe;
}
but I guess you want AnzahlPlaetze whatever it is so int getWartezimmer you could simply do this
vector<AnzahlPlaetze> Station::getWartezimmer()
{
vector<AnzahlPlaetze> result;
for(const auto& ob: WartezimmerListe)
{
result.push_back(ob.getAnzahlPlaetze());
}
return result
}
Your code is working exactly as it should work, the function getWartezimmer() returns something as soon as it finds it.
In this code
int Station::getWartezimmer(){
list<Wartezimmer>::iterator I;
for (i = WartezimmerListe.begin(); i != WartezimmerListe.end(); i++){
return i->getAnzahlPlaetze(); //--> this line
}
return 0;
};
at the lined I've marked, the function getAnzahlPlaetze() fetches a value and returns it to a returns statement. What happens then? the return statement returns this value to the main function and the execution continues from there. So every time you call this function, it will always return the firs value of the WartezimmerListe vector.
I don't know the exact behavior that you are looking for after calling getWartezimmer(). do you wanna pop an element from the beginning of the array or from its end?
You can edit the getAnzahlPlaetze() function to return an element and then delete it or use std::vector::pop_back() if it matches your needs. This will effectively give you a new value for each call.
If you want to maintain the original vector but also poping values from it you can define a static variable inside the class and use it to index the vector.
I don't think you want getAnzahlPlaetze() to return the whole values of the vector because it makes no sense. You can use the vector itself.

What is the most efficient way to return results from recursion?

There are 2 possible ways that I am familiar with while returning a boolean/integer value from a recursive function that defines is the operation carried out was a success or not.
Using static variables inside the recursive function. Changing values in the recursive calls and then returning the final value once everything is done.
Passing the result variable by reference to the recursive function and then manipulating its values in the function and then checking if the value corresponds to the result or not.
void Graph::findPath(string from, string to)
{
int result = 0;
if (from == to) cout<<"There is a path!"<<endl;
else
{
findPathHelper(from, to, result);
if (result) cout<<"There is a path!"<<endl;
else cout<<"There is not a path!"<<endl;
}
}
void Graph::findPathHelper(string from, string toFind, int &found)
{
for (vector<string>::iterator i = adjList[from].begin(); i != adjList[from].end(); ++i)
{
if (!(toFind).compare(*i))
{
found = 1;
break;
}
else
findPathHelper(*i, toFind, found);
}
}
Is there a better way to achieve this?
Thank You
I have changed your implementation to use a return value
bool Graph::findPathHelper(const string& from, const string& toFind)
{
for (vector<string>::iterator i = adjList[from].begin(); i != adjList[from].end(); ++i)
{
// I have assumed you comparison was incorrect - i.e. toFind == *i is that you want
// toFind == *i - The two strings are equal - Thus found
// or
// Recurse on *i - Have we found it from recursion
if (toFind == *i || findPathHelper(*i, toFind)) {
return true;
}
}
// We have searched everywhere in the recursion and exhausted the list
// and still have not found it - so return false
return false;
}
You can return a value in the recursive function and use that returned value for checking if it was success or not in subsequent calls.
Using static variable for this purpose may work but it's generally not a good IDEA and many consider it as bad practice.
Look into the below link which explains why we must avoid static or global variables and what kind of problems it could lead to during recursion.
http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/recursion2.html
Note: I do not have enough reputation still to make a comment; and therefore i have posted this as answer.

bool method inside a class ''Control may reach end o non-void function''

So, i have a class called Vuelo, it has a method in which i can add a passenger to an airplane flight, i must check that the passenger id is not already in the array (the array is at first with all zeros), i must also check that there is enough space for another passenger to be added (max 10)
bool Vuelo :: agregarPasajero(int id)
{
int i = 0;
for(int iC = 0; iC < 10; iC++)
{
if (listaPasajeros[iC] == id)
{
i++;
return false;
}
}
if(i == 0)
{
if(cantidadPasajeros >= 10)
{
return false;
}
else
{
return true;
cantidadPasajeros++;
}
}
}
If i is not zero, you get to the end of the function without any kind of return statement. Since you declared the function to always return a bool, you should provide one for that case.
Now, you may know that i will never be zero at that spot, but the logic for that is fairly complex (I missed it on the first reading), and a compiler cannot be expected to realize that there is in fact no chance of control flow ever getting to the end of the function without encountering a return. In this case it's best to add a dummy return.
You can probably get away with not having a dummy return if you remove the bogus i == 0 test. i will necessarily always be zero at that point, since if it were ever increased, the function immediately returns false.
The statement cantidadPasajeros++; will never be executed since it is located after a return statement. Any halfway decent compiler also warns on that.

How to check if previous return is the current one?

I have the following code:
EDIT
....int l=0
char* FantasiaGameUtils::GetCurrentTask(char *chat_arg)
{ // I know the code looks disgusting but please ignore that as I am only testing now
static char *szTemp1="";
for(int i=0; i<loop; i++)
{
if(task[i][0]!=0)
{
sif (l==0)
{strcpy(chat_arg, task[i]);
}
else
{strcpy(chat_arg, task[i+1]);
}
strcpy(task[i], "");
if (szTemp1!="")
{
MessageBox(0, szTemp1, "szTemp Value", 0);
MessageBox(0, chat_arg, "Initial Value", 0);
return chat_arg != szTemp1 ? chat_arg : NULL;
} else {
l++;
szTemp1 = chat_arg;
MessageBox(0, szTemp1, "szTemp1 Value", 0); // #execute
return chat_arg;
}
}
strcpy(chat_arg, "");
}
return false;
}
EDIT
What I am trying to do is make the return sequence check the previous returned value, and if its not the same as the current one, then return the current value chat_arg, if it is, return NULL.
The purpose of this function is to get the users prompt from the global_chat. An example of a prompt would be !create <game-type> <game-time> <max-players>
This prompt would be individually stored by each word enter (after each space) into the task[][] array, respectively.
From there, this function goes into play. It should copy the current task value (the current word) into the chat_arg variable.
The problem I am experiencing is, for some reason every in my main game loop. The value of chat_arg is kept through-out the game loop. I want to clear this value. I've come up with this method of which I will check if the current value in the function is the same as the previous one and if it is, return NULL (clearing the value), if it's not, then there is no reason to clear the previous value so return the current value.
Might anyone have a better or improved way I can perform this check? Or can anyone PLEASE tell me how I can get the previous returned value from memory and check it against the current value? Thank-you!
1) Add C++ tag to your post, because it is not a pure C code.
for(int i=0; i < loop; i++) // This line will not compile using C compilers.
2) Better return 0 in below line. false does not match to char* you expect.
return false; -> return 0;
My solutions for your problem:
1) Create global variable and your local chat_arg compare to it.
extern char* g_prev_chat_arg = 0;
...
char* FantasiaGameUtils::GetCurrentTask(char *chat_arg)
{
...
//return chat_arg != /*previous return value*/ ? chat_arg : NULL;
if (0 == strcmp(chat_arg, g_prev_chat_arg))
{
return 0;
}
else
{
g_prev_chat_arg = chat_arg;
return chat_arg;
}
...
}
2) Create local static variable within your function char* FantasiaGameUtils::GetCurrentTask(char *chat_arg)and use it like above,
3) Store somewhere in code last returned value from char* FantasiaGameUtils::GetCurrentTask(char *chat_arg) and then pass it each time when you are calling GetCurrentTask(...)
char* FantasiaGameUtils::GetCurrentTask(const char* prev_chat_arg, char* chat_arg)

logical comparison operator

Here is my code for my logical comparison operator (==) overloaded. I use this to check if two strings are identical in size and content. It should return false otherwise.
bool MyString::operator==(const MyString& other)const
{
if(other.Size == this->Size)
{
for(int i = 0; i < this->Size+1; i++)
{
if(&other == this)
return true;
}
}
else
return false;
}
When I ran valgrind it told me warning control reaches end of non-void function. Any suggestions on how to fix this issue and what I could do to better the code?
When control reaches the end of your for loop, you immediately get to the end of the function without returning a value.
It looks to me like you have the logic in your for loop munged anyway -- it's comparing the address of the other item to this. While it's sort of okay to do that, you only need to do it once, not in a loop.
In the loop, you undoubtedly want to compare the characters in the string, not the addresses of the objects.
Edit:
A typical implementation would be something on this general order:
class MyString {
char *data;
size_t length;
public:
// ...
bool operator==(MyString const &other) const {
if (length != other.length)
return false;
for (int i=0; i<length; i++)
if (data[i] != other.data[i]) // If we see any inequality
return false; // they're not equal
return true; // all equal, so the strings are equal.
}
};
It's not too clear what determines equality if the sizes are equal, but
the loop suggests that you're looking for something like:
bool
MyString::operator==( MyString const& other ) const
{
return size == other.size && std::equals( ??? );
}
Well, first of all, if you enter the for loop, and the condition &other == this will not be met, you will never return anything. To fix this, you should just remove the else statement. This will cause your function to return false either if the other.Size == this->Size condition is not met, or if you've gone through the whole loop, and have not used return inside of it.
The second problem is the line if(&other == this). I believe that inside of the loop you intend to check all the symbols of the strings. But now you are only checking the pointer to the class itself. To check the characters, you will need to use something like if( other->data == this->data ), provided you have a data member in which you store the...data (sorry for tautology).
Another little flow is in the design. You see, to check that the strings are equal, you need to look through each and every character and check that they match. However, to prove the strings are not equal, you need to find just 1 pair of characters that does not match. After that, it is pointless to continue comparing. So it is better to changee your condition in the cycle to a negative one, in order to stop comparing immediately after you fuond a pair that does not match, and not to do useless comparations of other characters.
Generaly, it is a good practice to return all the errors as fast as it's possible and avoid unneeded cumputation. So if you can check something in the begining of your function with a simple check, better do it.
So, after all, you should have something like this:
bool MyString::operator==(const MyString& other)const
{
if(other.Size != this->Size)
return false;//If the sizes do not match, no need to check anything else. Just return false.
//If we are here, the sizes match. Lets check the characters.
for(int i = 0; i < this->Size+1; i++)
{
//If some pair doesnt match, the strings are not equal, and we exit.
if( other->data[i] != this->data[i])
return false;
}
//If we are here, all the characters did match, so we return true.
return true;
}
Just get rid of the else. This way there is a "default" behaviour returning false if the condition is not met. It's the functionality you intend, and the compiler or syntax checker won't complain.