Getting error in C++ code (vector implementation) [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 6 years ago.
Improve this question
I am writing C++ code where I am using an array of vector in the form :
vector<int> s1[k];
In some compiler, I am not getting any error but in some compiler, I am getting error:
Compile time error (error: ISO C++ forbids variable-size array 's1'
compilation terminated due to -Wfatal-errors.)
I don't understand why it's behaving differently in different compilers and how do I fix this problem?

Your k is probably not constant. C++ doesn't allow variable length arrays like C does.
Use
std::vector<std::vector<int>> s1(k) instead, if it is supposed to be dynamic array.

Related

C++ Array Error: "variable-sized object may not be initialized" [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
Any idea why this isn't working in C++? I feel like it's really simple, but I can't figure it out:
int main() {
int arraySize = 5;
char testArray[arraySize] = {'a', 'b', 'b', 'c', 'd'};
}
It works fine when I hard code 5 as the array size, but it doesn't like it when I use a variable name instead. Ultimately I'm trying to figure out how to write a function that deletes duplicate characters from the array, but I can't even get the array initialized.
By default compilers are expecting mix of C++ and C code.
So VLA is enabled as an extension. It is possible to enfore pure C++ then this code will fail to compile.
Note that msvc supports old C standard where VLA was not available yet, so in many multi-platform projects compilers are configured not to use VLA.
Now this warning:
variable-sized object may not be initialized
Basically says, you have requested VLA and you are initializing it with 5 elements.
It may happen that arraySize variable can be changed during runtime to value less then 5 and this can corrupt your initialization code. That is why this warning is reported. Some initialization may be uncompleted.
Now your question was closed as duplicate indicating that C++ standard do not support VLA. I reopened it since question was about warning what is not covered by duplicate.

why do i keep getting vector cannot name type error in C++? [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 3 years ago.
Improve this question
I keep getting the error "vector does not name type" from one of my classes each time I try to compile my code.
#ifndef DISK
#define DISK
#include "PageTableEntry.h"
#include <vector>
class disk{
private:
Vector <PageTableEntry*> frames;
public:
void addFrame(int Location, PageTableEntry* pte);
void removeFrame(int pteLocation);
Disk();
};
#endif
You should quote errors verbatim. I assume the error is actually more along the lines of Vector does not name type.
You have either not included the declaration for Vector in your code, doing so would provide the compiler with a type, or you have (more likely) mistakenly written Vector when it should be std::vector. Letter case and namespaces matter in C++.
Try:
std::vector<PageTableEntry*> frames;

C++ error: decomposition declaration not permitted in this context [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
Why is g++ giving an error like this?
blahblah.h:80:10: error: decomposition declaration not permitted in this context
float[NUM_OUTPUTS] output_buffer;
(Already solved, but creating this because there's no good google hits for this error text, and the error message is inscrutable.)
In C++ declarations, the array size goes after the variable name, not after the type:
float output_buffer[NUM_OUTPUTS];

Error using a lambda function for sort in C++ [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 trying to sort a std::vector (containing indexes of another std::vector).
sort(inds.begin(), inds.end(), [&](int i1, int i2) { return compares[i1] < compares[i2]; } );
But it keeps getting me the following error:
I fixed it myself adding -std=c++11 to g++ command.
Read your error message carefully, especially the first line.
Then compare the versions of your toolchain on the two computers and check whether that might cause the difference in the default C dialact being assumed or whether you have different default flags for building in your environment variables or build environment or similar.

C-style static array as a member of a template class - won't compile [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
I was practising using C++ templates (sounds way better than "playing around") by coding something that would basically be my own implementation of std::array when I stumbled across an odd compilation failure. I reduced the problem into the following class, which fails to compile on gcc version 4.9.0:
template <typename TestType>
class TestClass
{
TestType[10] data;
};
What am I doing wrong here?
The array part of the type should go after the identifier:
TestType data[10];