• Advertisement
Sign in to follow this  

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

This topic is 968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ? Edited by rip-off
mod edit: please don't mark threads as solved.

Share this post


Link to post
Share on other sites
Advertisement

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

Edited by Bacterius

Share this post


Link to post
Share on other sites

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};

Share this post


Link to post
Share on other sites

@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};

Share this post


Link to post
Share on other sites
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";

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

 

@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 ?

Edited by louie999

Share this post


Link to post
Share on other sites

 



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.

Edited by SimonForsman

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement