Why can't I access the vector? [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 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.

Related

C++ unexpected output [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 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)
{
}

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())

C++ "?>" operator [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 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.

"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.

C++ error: "expected constructor, destructor, or type conversion before "(" token [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 8 years ago.
Improve this question
Having trouble at the start of this assignment. Im sure it is a very basic error, hopefully a fresh pair of eyes can help. I am getting the above code in my .cpp class file. I have attached the .cpp and the header. Error is in line 7 of the .cpp.
Any help would be appreciated.
#include "Encryptor.h"
Encryptor::Encryptor(){
}
Encryptor::Encryptor(key, plainText)
{
newKey = key;
newPlainText = plainText;
cout << newKey << "/t" << newPlainText << endl;
}
Encryptor::~Encryptor()
{
//dtor
}
/*string Encryptor::getEncryption(){
return encryptedFile
}*/
header:
#ifndef ENCRYPTOR_H
#define ENCRYPTOR_H
#include <iostream>
#include <string>
using std::string;
class Encryptor
{
public:
Encryptor();
Encryptor(string, string);
virtual ~Encryptor();
//Accessor Function
string getEncryption() const;
private:
string newKey;
string newPlainText;
};
#endif // ENCRYPTOR_H
Line 7 should be:
Encryptor::Encryptor(string key, string plainText)
As you need to include types for the arguments.
You're not specifying the types for this constructor's definition
Encryptor::Encryptor(string key, string plainText)
^^^^^^ ^^^^^^
{
newKey = key;
newPlainText = plainText;
cout << newKey << "/t" << newPlainText << endl;
}
Furthermore this is not defined but just declared (as far as its definition stays commented out)
string getEncryption() const;
(and even that commented out definition lacks the const qualifier)