In Lua I can do it like this:
myTable = {1, 2, 3}
But how about in C++? is it possible to do that too ?
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.”
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};
std::vector<int> numbers = { 1, 2, 3 };
int numbers2[3] = { 1,2,3 };
std::map<std::string,std::string> fruits;
fruits["apple"] = "red";
Games/Projects Currently In Development:
Discord RPG Bot | D++ - The Lightweight C++ Discord API Library | TriviaBot Discord Trivia Bot
Did you remeber to #include <vector> ?@Mussi, :( I tried the std::vector but it gave me this error:
The code I tried was: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)) ===|
std::vector<int> a = {1, 2, 3};
Games/Projects Currently In Development:
Discord RPG Bot | D++ - The Lightweight C++ Discord API Library | TriviaBot Discord Trivia Bot
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 '{...}'
Games/Projects Currently In Development:
Discord RPG Bot | D++ - The Lightweight C++ Discord API Library | TriviaBot Discord Trivia Bot
Did you remeber to #include <vector> ?@Mussi, I tried the std::vector but it gave me this error:
The code I tried was: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)) ===|
std::vector<int> a = {1, 2, 3};
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:
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.