Advertisement

How would I be able to do this in C++?

Started by August 29, 2015 10:05 AM
11 comments, last by louie999 9 years, 4 months ago
Is there a way to make "tables" in C++ ?
In Lua I can do it like this:
myTable = {1, 2, 3}
But how about in C++? is it possible to do that too ?

A Lua "table" is just a generic associative mapping, which maps a set of keys to a set of values. In this special case it also happens to be what is known as a list or array in most programming languages. Read up on datastructures and you should find that C++ also has a notion of arrays, lists, mappings, ...

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Advertisement

If you need a fixed size array you can do:

int array[] = {1, 2, 3};

If you need a dynamic array, one that can change in size, use std::vector:

std::vector<int> vector = {1, 2, 3};

@Mussi, :( I tried the std::vector but it gave me this error:


C:\Games\SFML Games\Test\main.cpp||In function 'int main()':|
C:\Games\SFML Games\Test\main.cpp|17|error: in C++98 'a' must be initialized by constructor, not by '{...}'|
C:\Games\SFML Games\Test\main.cpp|17|error: could not convert '{1, 2, 3}' from '<brace-enclosed initializer list>' to 'std::vector<int>'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

The code I tried was:


std::vector<int> a = {1, 2, 3};
There are several approaches:

std::vector<int> numbers = { 1, 2, 3 };

int numbers2[3] = { 1,2,3 };

std::map<std::string,std::string> fruits;
fruits["apple"] = "red";

@Mussi, :( I tried the std::vector but it gave me this error:

C:\Games\SFML Games\Test\main.cpp||In function 'int main()':|C:\Games\SFML Games\Test\main.cpp|17|error: in C++98 'a' must be initialized by constructor, not by '{...}'|C:\Games\SFML Games\Test\main.cpp|17|error: could not convert '{1, 2, 3}' from '<brace-enclosed initializer list>' to 'std::vector<int>'|||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
The code I tried was:
std::vector<int> a = {1, 2, 3};
Did you remeber to #include <vector> ?
Also what compiler and version are you using, it looks like it doesn't support c++11 initializer lists...
Advertisement



Did you remeber to #include ?

He is probably not using C++11.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

He is probably not using C++11.


in C++98 'a' must be initialized by constructor, not by '{...}'


Yeah here's the crux of the problem mainly the bit about c++98...

@Mussi, sad.png I tried the std::vector but it gave me this error:


C:\Games\SFML Games\Test\main.cpp||In function 'int main()':|C:\Games\SFML Games\Test\main.cpp|17|error: in C++98 'a' must be initialized by constructor, not by '{...}'|C:\Games\SFML Games\Test\main.cpp|17|error: could not convert '{1, 2, 3}' from '<brace-enclosed initializer list>' to 'std::vector<int>'|||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
The code I tried was:

std::vector<int> a = {1, 2, 3};
Did you remeber to #include <vector> ?
Also what compiler and version are you using, it looks like it doesn't support c++11 initializer lists...

Ofcourse I didn't forget to #include <vector>, compiler:

Se5XH98.png

I'm also using Code::Blocks.

Btw, how can I use C++11 ?



Did you remeber to #include ?

He is probably not using C++11.

The error message implies that he is compiling in C++98 mode using a compiler that also supports later versions, probably gcc

passing -std=c++11 to the compiler should fix that, in code::blocks there should be a checkbox under settings>compiler>compiler flags that he can check.

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement