Why do variables in my struct appear as const? [duplicate] - c++

This question already has answers here:
Range-based for with brace-initializer over non-const values?
(5 answers)
Closed 2 years ago.
So I have a struct to make storing and comparing of x y coordinates easier.
I'm trying to get two sets of coordinates, so four numbers in total, as input from the user and stored in two 'Card_position' structs but I can't seem to get it to work from within the for loop.
On the for loop line it gives me an error: binding value of type 'const int' to reference to type 'int' drops 'const' qualifier
even though nothing has a const qualifier.
My solution was to just do it without the loop but I would like to know the real solution.
Here's a snippet of where the error is coming from.
Card_position guess1{0, 0};
Card_position guess2{0, 0};
while (true)
{
cout << player.get_name() << ": " << INPUT_CARDS;
string input = "";
for (int& guess : {guess1.x, guess1.y, guess2.x, guess2.y} )
{
cin >> input;
if(input == "q")
return 1;
guess = stoi_with_check(input);
}
I tried without the reference but then it just won't save the values into the structs. Any solutions?
here is the struct definition:
struct Card_position
{
int x;
int y;
bool operator ==(Card_position& other)
{
return (this->x == other.x) && (this->y == other.y);
}
bool operator <(Card_position& minimum)
{
return (this->x < minimum.x) || (this->y < minimum.y);
}
bool operator >(Card_position& max)
{
return (this->x > max.x) || (this->y > max.y);
}
};

The problem is the initializer list ({guess1.x, guess1.y, guess2.x, guess2.y}) that stores those ints internally as const, and then the int & cannot bind to that value. You can get around this by using an `std::reference_wrapper':
for (auto guess : {std::ref(guess1.x), std::ref(guess1.y), std::ref(guess2.x), std::ref(guess2.y)} )
{
cin >> input;
if(input == "q")
return 1;
guess = stoi_with_check(input);
}
The guess is then intialized by a copy of that wrapper, that can be manipulated and will be able to edit the value it points to. (Alternatively, you can use pointers like this:
for (int* guess : {&guess1.x, &guess1.y, &guess2.x, &guess2.y} )
{
cin >> input;
if(input == "q")
return 1;
*guess = stoi_with_check(input);
}

Because an initializer list gives a const iterator.
This is creating an initializer list:
{guess1.x, guess1.y, guess2.x, guess2.y}
I’m not really sure what the point of this construction is anyways because assigning to these values makes little sense, since you can't retrieve them after the for loop.
Maybe you are looking for something like this?
std::vector<int> v{guess1.x, guess1.y, guess2.x, guess2.y};
for (int& guess : v)
{ // ...

Related

How to overload == operator to see if two objects with a string vector are equal?

I am writing a class named StringSet which has vector<string> data and int length as its private members.
bool StringSet::operator == (StringSet d)
{
for (int i = 0; i < length; i++)
{
if (data[i] == d.data[i])
{
return true;
}
}
return false;
}
When I try calling this function like this,
StringSet doc1, doc2;
if (doc1 == doc2)
{
cout << "Both sentences are identical!\n";
}
I get an assertion failure saying vector subscript out of range, I know what that means but I don't know how it implies here. If anyone can point out an obvious mistake I have made that would be great as I am a newbie to c++.
It's simple
bool StringSet::operator == (const StringSet& d) const
{
return data == d.data;
}
std::vector and std::string have already comparison operators, therefore you don't have to implement something special.

Having issues with overloading C++ operators

I'm having some issues understanding this concept. In the main.cpp file, we have a function as follows:
void TestComparison()
{
MyFloat X, Y;
cout << "\n\n============ Testing \"==\" for MyFloat ================\n";
do
{
cout << "\nEnter X ==> ";
X.Read();
cin.ignore(1000, '\n'); // Discard all chars in input stream.
cout << "\nEnter Y ==> ";
Y.Read();
cin.ignore(1000, '\n'); // Discard all chars in input stream.
cout << "\n\n";
if ( X == Y )
{
X.Write(); cout << " is equal to "; Y.Write();
}
else
{
X.Write(); cout << " is NOT equal to "; Y.Write();
}
}
while ( SpaceBarToContinue() );
}
This is the class I'm writing:
class MyFloat
{
enum {MAXDIGIT=20};
char Number[MAXDIGIT+1];
char NumberOfDigits;
public:
friend void AssignValue(MyFloat& X);//remove after the program works
MyFloat();
int Digits();
int MaxDigits();
void Read();
void Write();
MyFloat operator + (MyFloat x);
int operator== (MyFloat x);
};
Here is my == overload function stub:
int MyFloat::operator== (MyFloat x)
{
int Flag=0;
return 1;
}
The only purpose of this is to compare two an array of objects X and Y. They are passed into a == overloaded function. I'm supposed to write the algorithm that compares them. I know how to write the algorithm that compares these two character arrays, thats not the issue, but what I'm failing to understand is how both X and Y get into the the overloaded function to compare them? In the main, the code ( X == Y ) is used to obtain a 0 or 1. How are X and Y passed into the function?
For instance, I would assume my function stub would need to be rewritten with 2 parameters:
int MyFloat::operator== (MyFloat x, MyFloat y)
{
int Flag=0;
return 1;
}
But doing this produces an error back in the main during the function call of ( X == Y ) that states 'Overload "operator==" must be a binary operator (has 3 parameters)'
So I'm totally confused on how to get both Objects of MyFloat into the function to compare them. I'm still fairly new to programming (5-6 months of learning), any plain and simple answers are greatly appreciated.
When you write:
if(a == b)
what it really means is:
if(a.operator==(b))
So in your method:
bool MyFloat::operator==(const MyFloat &x) const
{
// x is b in call above
// (*this) is a in call above
// Your class invariant should guarantee this:
// assert(x.NumberOfDigits < MAX_DIGITS);
// In the scope of your class' methods:
// NumberOfDigits corresponds to this->NumberOfDigits
// Number corresponds to this->Number
if(x.NumberOfDigits != NumberOfDigits) return false;
// Same as: if(x.NumberOfDigits != this->NumberOfDigits) return false;
return strncmp(x.Number, Number, NumberOfDigits) == 0;
// Same as: return strncmp(x.Number, this->Number, this->NumberOfDigits) == 0;
}
Note that I changed the signature of your method. The correct signature returns a bool and takes a const (because you don't want to change the parameter) reference (avoid copying a big object) as parameter. The method is (and must be) const because it's not supposed to modify the object and it must be callable on a const object.
Note that it is possible to define the operator as a non-member function (i.e outside of the class) with the following signature:
bool operator==(const MyFloat &a, const MyFloat &b)
You should use this pointer. For more information: Source
bool MyFloat::operator==(const MyFloat& x) const
{
for(int i = 0; i < x.MaxDigits; ++i)
{
if(x[i] != (*this)[i])
return false;
}
return true;
}
member functions (including overloaded operators) have an implicit this parameter passed in. In your case since you are using a member version of operator== you should only need one parameter the other is this.

Taking in array of unknown size in c++

Ok I am extremely new to programming, and I am taking a c++ class. Basically for the current project I have to take in an array of unknown size, resize it, and output a bunch of statistics like average, Q1, Q3, etc. I am having trouble taking in the array from the user. I need to quit taking in variables once they enter 0. Here is what I have:
int i = 1; //When I first posted this I didn't mean to comment out the '= 1' part
do {
cin >> array[i];
if (array[i] != 0)
return true;
} while (true);
What am I doing wrong? the program stops after I enter 1 number every time no matter what number I enter.
I am using vector class btw.
Do the following:
// change int to your type
int val;
std::vector<int> vec;
while(std::cin >> val) {
if(val == 0) break;
vec.push_back(val);
}
Reason: Stating a return clause causes to exit the loop.
use of std::vector ensures the arbitrary size condition.
Update after #nonsensickle's constructive remark:
The following piece of code also ensures the only 0 terminates input process condition:
// change int to your type
int val;
std::vector<int> vec;
do {
if(std::cin >> val) {
if(val == 0) break;
vec.push_back(val);
} else { // fix broken input stream in case of bad input
std::cin.clear();
std::cin.ignore(1,'\n');
}
} while(true);
and a more sophisticated way, although overkill but what the hell :), with templates and type traits:
template <typename T>
struct zero_traits
{
static T getzero() { return T(0); }
};
template <>
struct zero_traits<std::string>
{
static std::string getzero() { return "0"; }
};
template <>
struct zero_traits<char>
{
static char getzero() { return '0'; }
};
template <typename T>
std::vector<T> read_values()
{
T val;
std::vector<T> vec;
do {
if(std::cin >> val) {
if(val == zero_traits<T>::getzero()) break;
vec.push_back(val);
} else {
std::cin.clear();
std::cin.ignore(1,'\n');
}
} while(true);
return vec;
}
int main()
{
// change int to your type
std::vector<int> vec = read_values<int>();
for(auto i : vec) std::cout << i << std::endl;
}
First of all i will never increment.
Second of all, if (array[i] != 0) will return if that array's value doesn't equal 0.
You need to read into how do { ... } while() loops work as well as what return statements do. Might as well throw in how to increment an array while you're at it.
I will not try to answer your question directly. What you have is a small logic error and a misunderstanding of the do {...} while () looping construct. What you need is to learn how to step through your code.
Let's go through your code line by line (there are only 6 lines here so it should be really easy):
int i; - Ok, so we are declaring an integer i here but are not giving it a value. As such, i can have a random value.
do { - This is where we will come back to when we evaluate the while clause. But only if the result of the while clause is true.
cin >> array[i] - Store a value that the user enters in the array at the position i. Here we ask ourselves a question, what is i? We should know its value without having to run the program. Hint: there's a problem here because of i
if (array[i] != 0) - If the number entered by the user is not zero return true (exit this function with the result true).
} while (true); - Go back to the do { line and redo all the steps until you get here. There is no condition here so it will keep happening until we exit this function.
Hint: The only exit point of your loop is at step 4.
With this, you should be able to figure out your problem. Trying to break down the problem for yourself should be your first step.
I recommend reading this blog post on debugging small programs. It should be informative.
Though code posted by others (in particular #DimitriosBouzas) will work, and is the better choice, I strongly recommend fixing your code and learning why it failed. This will help you in the long run more than #DimitriosBouzas' elegant solution.
Before answering your question.
Initialize your variables int i=0; .You assign i to be zero because arrays are zero indexed.
You have to incerement i. If do not increment it, i will point at the first "bucket" in your array the whole time. Use i++ or i = i + 1 after every iteration of the do while loop to move "forward" in your array.
You want your program to run until zero is entered so you have to write your condition like this if (array[i] == 0) return true;. This condition is true when the last number entered was zero and it will cause your method to return. It would be more elegant for you to check for it in the while clause.
Putting it all together, your code should look like this
int i=0;
do {
cin >> array[i];
if (array[i] != 0) break;
i++;
} while (i < maxSize);
//do stuff with filled array

Is it possible see the value returned in c++?

So..I'm testing a function with assert: (The value of pBola1 is 1)
assert(BomboTest.TreureBola(1)==pBola1);
BomboTest.TreureBola it's a function that returns a random number (in this case has to return 1) of a list.
cBola* cBombo::TreureBola(int num)
{
int posicio_aleatoria;
posicio_aleatoria= rand() % (num);
return(Boles.TreureElement(posicio_aleatoria));
}
And TreureElement it's a function that returns an element of a dynamic list knowing the position of the element that you want to extract(in this case returns 'retorn' which is 1)
cBola* cLlista::TreureElement(int posicio)
{
int i;
cBola* recorreLlista;
cBola *retorn;
recorreLlista=primer;
retorn = primer;
i=0;
if (posicio == 0)
{
primer = (*primer).getSeguent();
}
else
{
// Busquem la posició //
while(i < posicio)
{
recorreLlista= retorn;
retorn = (*retorn).getSeguent();
i++;
}
(*recorreLlista).setSeguent( (*retorn).getSeguent() );
}
numElements--;
return retorn;
}
And I don't know why but the assert fails. I can see the value returned by TreureElement because I have the pointer 'retorn' but I can't know the value returned by TreureBola..There is some way to see that value returned by TreureBola in the debugger?
PD:I'm using visual studio 2010
Just create a local
cBola* pTemp = BomboTest.TreureBola(1);
assert(pTemp==pBola1);
You could look in the dissasembly and inspect the return registry, but this seems like overkill. The above is the correct approach and others will thank you in the future, when they encounter the same problem.
You can always temporarily change
assert(BomboTest.TreureBola(1)==pBola1);
to`
auto tmp=BomboTest.TreureBola(1);
assert(tmp==pBola1);
and place a breakpoint on the first line.
I would write a small wrapper around assert to use instead:
template <typename T>
void compare(const T& lhs, const T& rhs)
{
if (lhs != rhs)
cout << "The values were not the same! " << lhs << " vs. " << rhs << endl;
assert(lhs == rhs);
}
This will still call assert, but first you'll get some (hopefully) useful output first.
So instead of calling:
assert(BomboTest.TreureBola(1)==pBola1);
You would call:
compare(BomboTest.TreureBola(1), pBola1);
This has an added benefit that you can place a breakpoint here and see see what TreureBola returned in the debugger, too.

Assigning Private Variable within a constructor

I've read through stack overflow threads multiple times in the past, and they're often quite helpful. However, I've run into a problem that simply doesn't make sense to me, and I'm trying to figure out what I missed. Here's the sections of the code that I'm having trouble with:
class BigInts
{
public:
static const std::size_t MAXLEN = 100;
BigInts(signed int i); //constructor
BigInts(std::string &); //other constructor
std::size_t size() const;
digit_type operator[](std::size_t ) const;
private:
digit_type _data[MAXLEN];
bool _negative;
int _significant;
};
//nonmember functions
std::ostream & operator << (std::ostream &, const BigInts &);
BigInts::BigInts(signed int i)
{
_negative = (i < 0);
if (i < 0)
{
i = -1*i;
}
std::fill(_data, _data+MAXLEN, 0);
if (i != 0)
{
int d(0);
int c(0);
do
{
_data[d++] = ( i % 10);
i = i / 10;
c++; //digit counter
}while(i > 0);
//_significant = c; //The problem line
assert(c <= MAXLEN); //checks if int got too big
}
}
std::size_t BigInts::size() const
{
std::size_t pos(MAXLEN-1);
while (pos > 0 && _data[pos] == 0)
--pos;
return pos+1;
}
std::ostream & operator << (std::ostream & os, const BigInts & b)
{
for (int i = (b.size() - 1); i >= 0; --i)
os << b[i];
return os;
}
int main()
{
signed int a, b;
std::cout << "enter first number" << std::endl;
std::cin >> a;
std::cout << "enter second number" << std::endl;
std::cin >> b;
BigInts d(a), e(b), f(b);
std::cout << d << " " << e << " " << f;
Major edit, switched from an attempted dummy version of the code to the actual code I'm using, complete with the original variable names. I tried to remove anything that isn't relevant to the code I'm currently working with, but if you see a strange name or call in there, let me know and I can post the associated portion.
The code had been working fine prior to the introduction of _significant, which is a variable I had added to add some more functionality to the class as a whole. However, when I attempted to drive the basic parts of it using the main function you see displayed, it encountered large errors. For example, I inputted 200 and 100 for a and b respectively, it outputted 201, 1, and 3 for d, e, and f. As it currently stands, the ONLY place _significant appears is when I'm attempting to assign the value of c to it.
The only error I can see right now is that _significant isn't initialized when the input is zero.
Step through it in a debugger, make sure the the right digits are ending up in the array and that the array data isn't being overwritten unexpectedly.
EDIT: It works for me (cleaned up slightly). More cleaned up, also working: http://ideone.com/MDQF8
If your class is busted purely by assigning to a member variable, that means stack corruption without a doubt. Whilst I can't see the source offhand, you should replace all buffers with self-length-checking classes to verify accesses.
The line i - 1; in the original code looks highly suspicious. Did you want to write i -= 1; or --i; or something else?
It decrements i by 1 and then throws away the result.