vectors sent from .cpp to .h file using classes and objects - Bug!

I get few bugs regarding my code.

Any ideas what approach can fix this problem ?

Background code info:
The purpose of this code is to use the ideas of inheritance to create .cpp file that sends stuff to .h files that each have access to a base .h file that includes virtual function_name const = 0;. This means they are upcon change in .h file, but all the basic stuff should be sent from the main() in .cpp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Errors:

g++   -c /home/k/Desktop/a.cpp -o /home/k/Desktop/a.o
/home/k/Desktop/a.cpp: In function ‘int main()’:
/home/k/Desktop/a.cpp:126:54: error: in C++98 ‘q5_options’ must be initialized by constructor, not by ‘{...}’
   vector<string>q5_options = {"15", "45", "570", "35"};
                                                      ^
/home/k/Desktop/a.cpp:126:54: error: could not convert ‘{"15", "45", "570", "35"}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<std::__cxx11::basic_string<char> >’

/home/k/Desktop/a.cpp:127:155: error: in C++98 ‘q6_options’ must be initialized by constructor, not by ‘{...}’
   vector<string>q6_options = {"Kola Superdeep Borehole, 12261 m", "Noma Everhole, 13428 m",  "Gabe Brittonhole, 9762 m", "The Bingham Canyon Mine, 4023 m"};
                                                                                                                                                           ^
/home/k/Desktop/a.cpp:127:155: error: could not convert ‘{"Kola Superdeep Borehole, 12261 m", "Noma Everhole, 13428 m", "Gabe Brittonhole, 9762 m", "The Bingham Canyon Mine, 4023 m"}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<std::__cxx11::basic_string<char> >’

/home/k/Desktop/a.cpp:128:88: error: in C++98 ‘q7_options’ must be initialized by constructor, not by ‘{...}’
   vector<string>q7_options = {"Wait, chickens cannot fly!", "9 sec", "16 sec", "13 sec"};
                                                                                        ^
/home/k/Desktop/a.cpp:128:88: error: could not convert ‘{"Wait, chickens cannot fly!", "9 sec", "16 sec", "13 sec"}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<std::__cxx11::basic_string<char> >’
Process terminated with status 1 (0 minute(s), 0 second(s))
6 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 


1
2
3
4
5
6
7
8
9
10
11
12
13
// .cpp file:

vector<string>q5_options = {"15", "45", "570", "35"};
vector<string>q6_options = {"Kola Superdeep Borehole, 12261 m", "Noma Everhole, 13428 m",  "Gabe Brittonhole, 9762 m", "The Bingham Canyon Mine, 4023 m"};
vector<string>q7_options = {"Wait, chickens cannot fly!", "9 sec", "16 sec", "13 sec"};

//...

// MC is the name .h file and its class, and use1 is the object's name
MC use1(q5, a5, s5, q5_options); // q: question , a: answer, s: score , q5_options: question 5 options 
MC use2(q6, a6, s6, q6_options);
MC use3(q7, a7, s7, q7_options);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// MC.h file

// Default constructor in public class:

		private:
			string question;
			char ca;
			int spq;
			vector <string> q_options;

		public:

			//default constructor: 
			MC(string q, char a, int s, vector <string> q_choices)
			{
				question = q;
				ca = a;
			        int spq = s;
				for (int i = 0; i < q_choices.size(); ++i)
				{
					q_options[i] = q_choices[i];
				}
			}


Last edited on
in C++98


So, can you bring things into this century and compile with c++11 or c++14?

g++ -std=c++14 -Wall -Wextra -pedantic-errors

One can find the version of g++ with:

g++ --version

Ideally update the compiler to the latest version (about 6.3.1) , the gcc website has some binaries to install.
Last edited on
OP: error messages suggest the problem lies in trying to initialize a std::vector<std::string> with a brace-enclosed initializer list <std::string>. Well that is possible but it came into the standard only C++11 onwards:
http://www.cplusplus.com/reference/vector/vector/vector/
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::vector<std::string> myVec {"hello", "world"};//technically string literals
    for (const auto& elem : myVec)std::cout << elem << " ";
}

As tIM suggests, you'd need a more recent compiler or IDE. Re IDE see here for some ideas:
http://www.cplusplus.com/forum/beginner/210594/
Last edited on
Topic archived. No new replies allowed.