Logical operator causing compile-time error on functions called - c++

This question might have been answered before, but searching around and using google didn't bring me there, so I'll ask.
I'm currently making a game and working on collision, however, for some reason it seems like whenever I try to compile I get a ISO C++ forbids comparison between pointer and integer
At first I thought I might have something wrong in my syntax which I checked, but wasn't able to find anything, so I just left the line of code to this:
if((getMinX() > c->getMinX()) && (getMinX() < c-getMaxX()))
I tried adding this-> or adding parenthesis, but that doesn't seem to work, however, just doing
if(this->getMinX() > c->getMinX())
seems to work fine as well as
if((5 > 3) && (5 < 10))
Is there something I'm missing?
Both objects are derived from a class called Collideable defined like this
class Collideable
{ public:
bool collidesWith(Collideable*);
virtual int getMinX() = 0;
virtual int getMaxX() = 0;
virtual int getMinY() = 0;
virtual int getMaxY() = 0;
};
All classes properly override from the virtual methods and the code causing the issue is in bool Collideable::collidesWith(Collideable* c)
According to http://www.cplusplus.com/doc/tutorial/operators/ the logical operators return a boolean value (which makes sense to me, coming from Java) so what's causing this problem?

It is difficult to know if this is what is in the program or a simple transcription problem:
The first line is written ending as c-getMaxX(); maybe it should be c->getMaxX()?

You have c-getMaxX() but you need c->getMaxX(), with a >.

Related

Cannot access member functions of vector<int>

I've been attempting to figure out this bug for about an hour now. It's probably a really obvious syntax thing I'm overlooking. This is my first C++ project, and I don't have a good handle on the structure of the language.
Here's my header file:
#pragma once
#include <vector>
class BoardState
{
private:
std::vector<int> numbers;
int SIZE;
public:
BoardState();
std::vector<int> getState();
bool isZero();
};
And here's the implementation, in a separate file:
#include "BoardState.h"
BoardState::BoardState(){
SIZE = 4;
numbers.push_back(1);
numbers.push_back(3);
numbers.push_back(5);
numbers.push_back(7);
}
std::vector<int> BoardState::getState() { return numbers; }
bool BoardState::isZero() {
for (int i = 0; i < numbers.size(); i++) {
if (numbers[i] != 0) { return false; }
}
return true;
}
This code is really simple, so I have no clue what could be going wrong to produce the errors. However, on every method call, push_back and size, I am getting errors, saying that class "std::vector<int, allocator>" has no member "method_name_here".
My background is Java, so my first thought was that I wasn't able to call these methods because numbers is not initialized. However, any attempt I made to initialize numbers in the header file resulted in an error as well. I tried std::vector<int> numbers = { 1,3,5,7 };, I tried std::vector<int> numbers(4,0);, I even tried creating an array and constructing the vector from that. Not only did all those attempts cause errors, they also didn't fix the method calls either.
What am I missing? Do I need to initialize the vector, or is what I have in the header file enough? Any advice would be helpful here, since I can't find anything online about similar errors. I've even copy-pasted code from StackOverflow answers about similar problems, and that produced errors as well.
EDIT: I've pared down the code as much as possible while keeping the error:
#include <vector>
class BoardState
{
std::vector<int> numbers;
BoardState() { numbers.push_back(1); }
int getSize() {
int i = numbers.size();
return i;
}
};
On the line numbers.push_back(1);, my compiler underlines the token "push_back", and highlighting it reads:
class "std::vector<int, allocator>" has no member "push_back"
On the line int i = numbers.size();, the token "size" is underlined, and the error reads:
class "std::vector<int, allocator>" has no member "size"
I still have no clue what's going on.
Edit 2: Put the method calls into a constructor and a function. This changed the error message associated with push_back().
Edit 3: I have discovered something very disconcerting. This code works perfectly fine in a different compiler. I copy-pasted in the exact code from Edit 1 and it ran with no issues. I think the problem must be with Visual Studio rather than the actual code. Thank you all for helping me out with this. I think I'm just going to switch to a different compiler and hope for the best.
Edit 4: Just to prove to pm100 that my code is exactly as I've said, here's a screenshot from visual studio.
Here it is.
Aside from the main method, this is character-for-character what I've put in this question. I have a guess as to why this doesn't work, though. I modified my version of Visual Studio 2019 to run .386 assembly code for a college class. While I think I followed the guide to do that without affecting anything else, it may have screwed up parts of the C++ compiler.
I suggest that you could select Tools->Import and Export Settings->Reset all settings-> Visual C++ to restore the default settings.
If it does not work, you could reinstall VS.

no viable overloaded '=' error, what may cause this?

Note: I have uploaded my code here: https://onlinegdb.com/r1P8APG0I you can run it and reproduce the bug.
I wrote an abstract Character class with is inherited by many classes like Soldier and Medic. also, I have a Game class which has:
mtm::Dimensions dimensions;
std::vector<std::shared_ptr<Character>> board;
Important Note: I can't change the code that's written above, so please consider that.
I need to write a copy c'tor for this class Which makes a copy that isn't related at all to the previous one after the call is finished, So I wrote:
Game::Game(const Game &other):dimensions(other.dimensions){
int board_size = dimensions.getRow() * dimensions.getCol();
for (int i = 0; i < board_size; ++i) {
Character* copy = other.board[i]->clone();
board[i]=*copy;
}
}
But I'm getting an error saying:
error: no viable overloaded '=' board[i]=*copy;
What does this mean and how may I fix it?
You may need the following implementation of clone over Medic class which inherits Character
Character * Medic::clone() const {
return new Medic(*this);
}
Update: I tried writing =copy instead of =*copy but got exactly the same error.
You are dereferencing a pointer here
board[i]=*copy;
If instead you are trying to construct a std::shared_ptr then create one using your raw pointer
board[i] = std::shared_ptr<Character>(copy);
which more concisely is just
board[i] = std::shared_ptr<Character>(other.board[i]->clone());

Undefined reference to function which is member of an another class

I'm building a simple application to solve sudoku puzzles. It's my first time creating something serious with C++, so I'm open for code style/structure critique.
The problem I bumped into has something to do with organizing multiple files.
I have two classes referencing each other using functions. When I try to call a function:
void Field::runColumnCheckout(CellGroup* sender, int cellRelativeX)
{
}
in a CellGroup class using an instance of Field class:
void CellGroup::runISC(int possibilityNumber)
{
for (int x = 0; x < 3; x++) {
int amountInColumn = 0;
for (int y = 0; y < 3; y++)
if (cells[x][y]->isPossible(possibilityNumber))
amountInColumn++;
if (amountInColumn > 1) {
//parentField is an instance of a Field class
//stored in a private field of the CellGroup class
parentField->runColumnCheckout(this, x);
return;
}
}
//...
}
An undefined reference occures. I don't quite understand, why.
All the examples were taken from cell_group.cpp and field.cpp that are defining classes from cell_group.h and field.h.
Unfortunately, I couldn't manage to put all the files in a question as they have gained a lot of lines, but you can look at them on my github.
I have found a similar question and another one, but they seem to have issue with the way they compile their files. I've had two referencing each other classes structured similarly before adding code I have problem with right now and everything compiled fine.
I'm compiling everything using GCC compiler on Windows and CodeBlocks as an IDE.
CodeBlocks somehow lost connection with field.cpp file or didn't add it creating file. Going and manually adding it to the project tree (even though it was already there) Project->Add files... solved the problem.
Thanks #Peter for finding the solution.

libxmlrpc iterate through struct

I am implementing libxmlrpc into C++ project, anywayy, my RPC server has returned a struct with 52 member structs in them.
I do not know what the keys are as they are opaque references, so I cannot rely on them.
How can I iterate through the struct, I would have thought it was with the following code:
XmlRpcValue param_array = XmlRpcValue::makeArray();
param_array.arrayAppendItem(XmlRpcValue::makeString(this->sessionKey));
param_array.arrayAppendItem(XmlRpcValue::makeString("petabytes"));
XmlRpcValue result = ServerCall("Charter.getDataWarehouse.storage.capacity", param_array, url);
int index = 0;
while(index < result.structSize())
{
XmlRpcValue Data = result.getStruct();
//Would have thought it would work with this ;( shit documentation libxmlrpc has, grrrr
//Data.structGetKeyAndValue(index);
//This for example works, because I know the opaque reference, but in real life I wont
cout << Data.structGetValue("OpaqueRef:d4e60db6-2271-b0ac-d362-1b51220980af").structSize() << endl;
index++;
}
However, Data.structGetKeyAndValue(index) errors with:
no matching function for call to 'XmlRpcValue::structGetKeyAndValue(int&)
Which is fine, I understand it's not a public (well I think it's not a public member function) of xmlrpcvalue, however I cannot find anything that would allow me to do this.
Anyone have any experience with this?
Some quick Googling seems to indicate that you've got the function signature wrong:
void XmlRpcValue::structGetKeyAndValue(const int index, std::string& out_key, XmlRpcValue& out_value);

C++ Function Pointer

Is this possible? If so, I can't seem to get the syntax right. (C++ function pointer)
bit of background. The code below has been shorten for this post. The reason for this implementation is to avoid an endless list of SWITCH/CASE or IF/ELSEIF statements; and have an endless list of DECODER_FUNCTION_TABLE (see below). This code deals with an industry standard that uses mnemonics to mean different things and there are hundreds of these mnemonics. So this portion of my code is to decode certain mnemonics pass to it from another section of code that loops through a passed in record... anyway my difficulty is in keeping a member function pointer in a structure outside of the class...
Have a look. I think the code may do a better job explaining ;)
typedef struct _DECODER_FUNCTION_RECS
{
ISO_MNEMONIC_ID Mnemonic;
void (Database::*pFn)(Database::Rec *);
}DECODER_FUNCTION_RECS;
DECODER_FUNCTION_RECS DECODER_FUNCTION_TABLE[] = {
SFP, &Database::Decode_SFP,
KOG, &Database::Decode_KOG
};
void Database::DecodedDescription(Rec *A)
{
int i = 0;
bool Found = false;
while( i < DECODER_FUNCTION_TABLE_COUNT && !Found )
{
if( DECODER_FUNCTION_TABLE[i].Mnemonic == A->Mnemonic )
Found = true;
else
i++;
}
if( Found )
(([DECODER_FUNCTION_TABLE[i]).*this.*pFn)( A );
}
void Database::Decode_SFP(Rec *A)
{
// do decode stuff on A
}
The detail I'm trying to work out is this line:
(([DECODER_FUNCTION_TABLE[i]).*this.*pFn)( A );
You call a member function pointer (that's what it's called) with
(this->*DECODER_FUNCTION_TABLE[i].pFn)(A);
Could put parens around DECODER_FUNCTION_TABLE[i].pFn, but the member access operator . has a higher precedence than member function operator ->*.
I wrote up a few simple examples that will shed some light the other day
It's in my answer to this question
error C2664 and C2597 in OpenGL and DevIL in C++
Or a direct link to codepad