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 a C++ beginner
I wonder if there is any chance you might be able to tell me why my code (below) produces unexpected output? It compiles fine.
./a.out produces 4.94066e-324 rather than 1000
Thanks very much. Esther
#include <iostream>
#include <string>
#include <vector>
enum class OrderBookType{bid, ask};
class OrderBookEntry
{
public:
OrderBookEntry(double price, double amount, std::string timestamp, std::string product, OrderBookType orderType)
{}
double price;
double amount;
std::string timestamp;
std::string product;
OrderBookType orderType;
};
int main()
{
while(true)
{
OrderBookEntry order1{1000,0.02,"2020/03/17 17:01:24.884492","BTC/USDT",OrderBookType::bid};
std::cout << order1.price << std::endl;
return 0;`
Your constructor is not initializing the values.
use Initialiser list like this
OrderBookEntry(double price, double amount, std::string timestamp, std::string product, OrderBookType orderType)
: price(price),
amount(amount),
timestamp(timestamp),
product(product),
orderType(orderType)
{
}
Related
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;}
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.
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())
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
class skClass {
public:
void setName(string x) {
name = x;
}
string getName() {
return name;
}
private:
string name;
};
int main() {
skClass sk;
sk.setName = ("Mr Bashir Sentongo");
cout << sk.getName() << endl;
return 0;
}
Methods are called by passing parameters within parenthesis (()), not with the assignment operator (=):
I.e., you should replace
sk.setName = ("Mr Bashir Sentongo");
With
sk.setName("Mr Bashir Sentongo");
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.