C++ "?>" operator [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
Could someone explain me what this kind of operator this?> does in C++?
Below example of code with usage of it:
#include <iostream>
#include <string>
using namespace std;
class A {
public:
int x;
};
class B : public A {
public:
B() {x=1;}
B(int x) {this?>x = x;}
};
int main()
{
B c1;
B c2(10);
cout << c1.x;
cout << c2.x;
return 0;
}

I think you try to say that -> becouse ?> it does not exist.
In the contex, im sure that you want ->.
You probably have a mistake when you´re typing.
PD: try to compile before ask

It's a typo. You might want to refer to ->.
this operator is used to access the property of the object calling that function of that class in which the functions is defined, and -> operator is used to access the properties of that object.

Related

c++ using bool in a class with set and get functions [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.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am trying to create a class with private attributes and change values with set and get functions. However i cant seem to change the bool value
class VideoGames {
private:
bool buy;
public:
bool get_buy(){return buy;}
void set_buy(bool b){b = buy;}
};
main(){
VideoGames g1;
double price;
cin >> price;
if(price <=100){
g1.set_buy(true);
}
else {
g1.set_buy(false);
}
cout<<"Buy= " << g1.get_buy()<<endl;
return 0;
}
this always prints out 0 no matter what the price is. What do i need to change?
You flip-flopped the assignment; b = buy; reassigns the parameter (which is then thrown away so nothing changes), you wanted buy = b; to reassign the attribute from the parameter.
when using the "=" sign, the right side gets put in the left side, so instead of:
void set_buy(bool b){b = buy;}
write:
void set_buy(bool b){buy = b;}

double free or corruption (fasttop), With casting pointers to map down a class [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
In trying to figure out a more complicated issue, where the static_cast<Derived*>(base_class) returns an object with random Derived class variables. In trying to create a simple example, I found the following code is buggy, so I was wondering why. Currently it returns,
2
*** Error in `./main': double free or corruption (fasttop): 0x0000000000d55c20 ***
...
Here's an online copy: C++ Online Compiler
Here's the code:
#include <iostream>
#include <memory>
#include <map>
class Base {
};
class Derived : public Base {
public:
float a = 2.0f;
};
std::map<int, std::unique_ptr<Base>> mapOfClasses;
int main() {
std::unique_ptr<Base> baseUP = std::make_unique<Derived>();
int key = 0;
mapOfClasses.emplace(key, move(baseUP));
auto it = mapOfClasses.find(key);
Base* basePtr = it->second.get();
Derived* derivedPtr = static_cast<Derived*>(basePtr);
std::cout << derivedPtr->a << std::endl;
delete basePtr;
delete derivedPtr;
return 0;
}
Problem is that you delete the pointer three times. Once in the destructor of unique pointer and twice explicitly. More than once is too much. The behaviour of the program is undefined.
Two of those times to delete through a pointer to base. That also results in undefined behaviour because the destructor of the base isn't virtual.

Why can't I access the vector? [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 5 years ago.
Improve this question
I am currently trying to access the vector defined as such:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
template<class T>
class file
{
public:
typedef vector<vector<T> > buffer;
};
int main()
{
file<double> test;
cout << test.buffer.size() << endl;
std::vector<pair<string, file<double> > > list_of_files;
for (const auto& [name, file] : list_of_files)
{
cout << file.buffer.size() << endl;
}
}
the error message I am getting is that scoping the buffer like I am currently doing is invalid?, but why is it invalid? I don't see a reason why it should be?
I am in the for loop trying to iterate between the inner and outer vector of the buffer, but since I can't scope it, I am not able to access? How do i access it?
The reason for the error is because the code declares buffer as a new type for vector<vector<T>>. If you want buffer to be a member of file, you can do so like this:
template<class T>
class file
{
public:
std::vector<std::vector<T>> buffer;
};
After changing that, main() should compile without errors.

non-standard syntax; use '&' to create a pointer to member. Accessing boolean variable [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 5 years ago.
Improve this question
I've been searching for a while on why this does not work. I have not gotten a clear answer.
Can anyone explain why trying to access this boolean variable and comparing it to another boolean variable won't work?
I also tried setting the rhs of the comparison to 0, and that got rid of the boolean/int error, but I'm still getting the error.
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() {
setWorking(true);
}
//Mutator
void setWorking(bool x) { working = x; }
//Accessor
bool getWorking() { return working; }
private:
bool working;
};
int main() {
MyClass alpha;
if (alpha.getWorking == true) {
cout << "its working\n";
}
else {
cout << "not working\n";
}
return 0;
}
In main function
if (alpha.getWorking == true)
should be
if (alpha.getWorking())

"inString not declared in this scope" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
Working on an implementation of the Shunting Yard algorithm and I keep getting this error (In function 'void InputString()': 'inString was not declared in this scope') when trying to compile (what little) code I have - not entirely sure what could be causing it.
#include <iostream>
#include <string>
#include <stack>
#ifndef SHUNTINGYARD_H
#define SHUNTINGYARD_H
class ShuntingYard {
public:
void InputString();
void OutputString();
int precedence(char A, char B);
void ShuntingAlgorithm();
private:
std::string inString;
std::string outString;
std::stack<char> operatorStack;
std::stack<char> tokenStack;
};
#endif // SHUNTINGYARD_H
void InputString() {
std::cout << "Please enter an expression: ";
std::cin >> inString;
}
I'm sure I'll feel really dumb when I find/someone explains the solution, but I can't figure it out at the moment.
when you do -
void InputString() {
std::cout << "Please enter an expression: ";
std::cin >> inString;
}
It is only a bare function and it is not a member of the class ShuntingYard.
Hence it is not recognizing "instring"
So make InputString as a member of the class and then define it like below-
void ShuntingYard::InputString() {
std::cout << "Please enter an expression: ";
std::cin >> inString;
}
NOTE: Always write #endif //SHUTTINGYARD_H at the end of the file. It makes your code more standard.
Try adding the class name as well as the :: operator before the function's implementation:
void ShuntingYard::InputString() {
This tells the compiler that you are implementing a function called InputString() of the class ShuntingYard. Otherwise it thinks you are trying to declare a separate function that is separate from ShuntingYard, and thus does not know about its private variables.