Use '&' to create pointer to member error [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am creating a game in c++ and I have called a function, however I am getting an error saying i need to insert an ampersand to create a pointer to a member. However, I am unsure where this needs to go...
string MouseAndCatGame::prepareGrid(){
//prepare a string that holds the grid information
ostringstream os;
for (int row(1); row <= SIZE; ++row) //for each row (vertically)
{
for (int col(1); col <= SIZE; ++col) //for each column (horizontally)
{
if ((row == cat_.getY) && (col == cat_.getX))
{
os << cat_.getSymbol(); //show cat
}
else
if ((row == mouse_.getY()) && (col == mouse_.getX()))
os << mouse_.getSymbol(); //show mouse
else
{
bool holePresent(underground_.findHole(col, row));
if (holePresent == true) // If there is a hole at that location
{
os << HOLE; // Show hole symbol
}
else
{
if ((row == nut_.getY()) && (col == nut_.getX()))
{
os << nut_.getSymbol(); //show mouse
}
else
{
os << FREECELL;//show free grid cell
}
}
}
} //end of col-loop
os << endl;
} //end of row-loop
return os.str();
} //end prepareGrid
The error is specifically the following:
Cat::getY': non-standard syntax; use '&' to create a pointer to member

It looks like you don't want a pointer to member. You just forgot the parentheses on your function call.
if ((row == cat_.getY()) && (col == cat_.getX()))

assuming getY/getX are methods of cat_ and not poorly named public variables,
this
if ((row == cat_.getY) && (col == cat_.getX))
should be
if ((row == cat_.getY()) && (col == cat_.getX()))

Related

Same code, one works one doesn't. What's different? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 12 months ago.
Improve this question
i'm an beginner programmer, this is my first time posting. I'm currently writing a snake game in c++. Most of the game wasn't so hard to implement but when it came to the tail of the snake the entire program broke. I spent like 2 hours trying to figure out what was wrong and then i decided to try to rewrite the problematic code.
From my understanding i haven't changed a thing but now it works. Can someone explain to me what changed?
Here is the code, the commented one is not working the other works fine:
else {
bool eCoada = false;
for (int s = 0; s <= ntail; s++)
{
if (tail[s].height == j && tail[s].width == k)
{ eCoada = true; break; }
}
if (eCoada == false) cout << " ";
else cout << "o";
}
/* else {
bool eCoada = false;
for (int s = 0;s <= ntail; s++)
{
if (tail[s].height==j && k==tail[s].width==k)
{ eCoada = true; break; }
if (eCoada==false) cout << " ";
else cout << "o";
}
}*/
Also i should mention that this code is a part of a function, if you want me to post the full code i will do so.
k==tail[s].width==k is not the same as tail[s].width == k. You may think you've written something like (k == tail[s].width) && (tails[s].width == k. But C++ doesn't automatically put in && operators like that. What actually happens is that the associativity of the == operator is left to right. So what that actually means is
(k == tails[s].width) == k
Assuming k and tails[s].width are ints, that means (k == tails[s].width) is a bool. The comparison between that and k will be checking if k is 0 or 1, instead of checking if it matches the width as intended.
Another difference is in the placement if your if(eCoada==false) line.
In your working code, it's after the for loop finishes, which means that it only executes once.
In your broken code, it's inside the for loop, which means that every time the loop executes it prints a space. It also means that because you break out of the loop immediately upon setting eCoada to true, you never execute the else branch and never print an o.
C++, nor any other language that I know of, doesn't allow multiple comparisons in one statement.
You can't say is x == y == z,
you must break them up.
if(x ==y && y == z)

If statement still going through when my condition should be false [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am attempting to write a Caesar encryption program. I have written two functions.
The first (sanitize) allows me to make sure that all strings are fully capitalized, here is the source code.
string sanitize(string message) {
for(int i; i < message.length(); i++){
message[i] = toupper(message[i]);
}
return message;
}
The second (caesar) encrypts the message given. Here is the source code for that as well.
string caesar(string c_message, char direction) {
if (direction = 'R') {
for(int j; j < c_message.length(); j++) {
if((int)c_message[j] + 3 > 90) {
c_message[j] = (char)(64 + (3 - (90 - (int)c_message[j])));
} else {
c_message[j] = (char)((int)c_message[j] + 3);
}
}
} else if (direction = 'L') {
for(int i; i < c_message.length(); i++) {
if((int)c_message[i] - 3 < 65) {
c_message[i] = (char)(91 - (3 - ((int)c_message[i] - 65)));
} else {
c_message[i] = (char)((int)c_message[i] - 3);
}
}
} else {
cout << "directions: 'L' or 'R'" << endl;
}
return c_message;
}
An example of execution :
int main(){
cout << sanitize("HELLO") << " " << (char)3 << endl;
cout << caesar("HELLO", 'L') << endl;
return 0;
}
The first if statement works, but the second does not.
if (direction = 'R') {
} else if (direction = 'L') {
These lines are wrong. = in C++ is an assignment operator and it sets the value of a variable in lefthand to the value of righthand. Then, it is evaluated to the new (righthand) value. Another point is that nonzero values are considered as true when used as condition.
You should use a comparision operator == instead of that like this:
if (direction == 'R') {
} else if (direction == 'L') {

Using if statement if not multiple [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I want to create a 5x5 array of '' with a hashtag in the center instead of '', but only when the user inputs either 'a' or 'b.' On the area I marked "RIGHT HERE" it doesn't work unless its ONLY 'a' / ONLY 'b', so what do I do? Thank you in advance!
#include <iostream>
using namespace std;
int main()
{
while (true){
///Variables:
char array[4][4]; //Playing field 5x5
char direc; //Direction player moves
for (int x = 0; x <=4; x++){
for (int y = 0; y <= 4; y++){
array[x][y] = '_';
if (direc != 'a' || 'b'){ ///RIGHT HERE!
array[2][2] = '#';
}
cout << array[x][y]; //Starts printing Board
if (y == 4){
cout << endl; //Cuts to next line on print if 4 in a column row
}
}
}
cin >> direc;
cin.get();
}
}
Did not check the required logic of your statement or of the other parts of your program, but your marked statement should be written as
(direc != 'a' || direct != 'b')
Your statement (direc != 'a' || 'b') will always evaluate to true, since 'b' as the second operand of the logical or operator || is an integer value > 0 (representing character b in some encoding) and therefore treated as true.
As Neil mentioned in the comment, ur 5x5 field is in fact 4x4, so u are only able to access array[0][0] up to array[3][3], while your X and Y are 4 at some point. You should use this instead:
char array[5][5]
so that u can access your array up to index 4
hope that helps you
#include <iostream>
using namespace std;
int main()
{
///Variables:
char array[5][5]; //Playing field 5x5
char direc; //Direction player moves
char player;
while (true) {
//get direc
cin >> direc;
cout << direc << "\n";
if (direc == 'a' || direc == 'b') { ///RIGHT HERE!
player = '_';
}
else {
player = '#';
}
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 4; y++) {
array[x][y] = '_';
if (x == 2 && y == 2) {
array[x][y] = player;
}
cout << array[x][y]; //Starts printing Board
if (y == 4) {
cout << endl; //Cuts to next line on print if 4 in a column row
}
}
}
}
}

C++ error returning a string from a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to return a string from the function solution() but I am getting the error below. Apologies if this is pretty basic but could anybody explain how to return the string. I understand that it is related to pointers.
error: could not convert ‘(std::__cxx11::string*)(& hexaDeciNum)’ from
‘std::__cxx11::string* {aka std::__cxx11::basic_string*}’ to
‘std::__cxx11::string {aka std::__cxx11::basic_string}’
string solution(string &S){
int n = stoi(S);
int answer = 0;
// char array to store hexadecimal number
string hexaDeciNum[100];
// counter for hexadecimal number array
int i = 0;
while(n!=0)
{
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--){
//cout << hexaDeciNum[j] << "\n";
if (hexaDeciNum[j].compare("A") ==0 or hexaDeciNum[j].compare("B") ==0 or hexaDeciNum[j].compare("C") ==0 or hexaDeciNum[j].compare("D") ==0 or hexaDeciNum[j].compare("E") ==0 or hexaDeciNum[j].compare("F") ==0 or hexaDeciNum[j].compare("1") ==0 or hexaDeciNum[j].compare("0") ==0 ) {
answer = 1;
}
}
if (answer == 1){
return hexaDeciNum;
}
else {
return "ERROR";
}
}
int main() {
string word = "257";
string answer = solution(word);
return 0;
}
hexaDeciNum is defined as string hexaDeciNum[100]. It is not a string - it is an array of 100 string instances.
You're attempting to return it from a function that should return string.
You should define hexaDeciNum as string hexaDeciNum; instead of string hexaDeciNum[100];. With that way, you can still indexing operator. However you can not use compare method anymore, because each element of string is a char. Instead use operator == like in the following for your piece of code.
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--){
//cout << hexaDeciNum[j] << "\n";
if (hexaDeciNum[j] == 'A' or hexaDeciNum[j]=='B' or
hexaDeciNum[j] == 'C' or hexaDeciNum[j] == 'D' or
hexaDeciNum[j] == 'E' or hexaDeciNum[j] == 'F' or
hexaDeciNum[j] == '1' or hexaDeciNum[j] == '0' ) {
answer = 1;
}
}
and please don't forget to compile it for c++ 11 with -std=c++11 option of compiler.

Collatz Conjecture C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I created this function (see below), that works perfectly; however, I was tasked in changing the function to take a parameter that does the same thing just for the function I passed into it.
int collatz(){
int temp, num;
//num = 0;
cout << "pick a number to turn into one:";
cin >> num;
temp = 0;
while(num != 1){
if(num%2 == 0){
num = num/2;
temp++;
}
else if(num&2 != 0){
num = (3*num) + 1;
temp++;
}
}
cout << num << "number of times run: " << temp;
return 0;}
I came up with this; however, it gives me an error:
error C3861: 'collatztwo': identifier not found warning C4554: '&' :
check operator precedence for possible error;
use parentheses to clarify precedence
int collatztwo(int a){
int temp, num;
//num = a;
temp = 0;
while(a != 1){
if(a%2 == 0){
a = a/2;
temp++;
}
else if(a&2 != 0){
a = (3*a) + 1;
temp++;
}
}
cout << "looped: " << temp;
return 0;}
From the errors OP listed in his comment:
error C3861: 'collatztwo': identifier not found
When you made your new function, did you include a prototype for it before the call site? You have to declare int collatztwo(int a); at the top of your source file, or move the function definition before where you use it.
warning C4554: '&' : check operator precedence for possible error; use parentheses to clarify precedence
As already mentioned in the comments, you need to include a pair of parentheses around a&2 or it will do the wrong thing. else if ((a&2) != 0){