Error using <vector>

I've been using code::blocks for I don't know how long and it's always worked just fine. All of a sudden it started throwing this "error: in C++98 'myVector' must be initialized by constructor, not by '(...)'".

I wish I knew why it was talking about C++98 instead of C++11. If anyone can lend me some insight, I'd greatly appreciate it.

Thanks
Without the code noone can tell anything about it
GCC still use C++98 by default. You can enable C++11 by passing -std=c++0x (or -std=c++11 in gcc 4.7). Note that no compiler have complete support for C++11 yet.

As coder777 says, we can't help you unless you show some code.
Somehow I got passed that error last night, but now it's saying that one of my references is undefined, but it appears pretty defined to me.

Here's the program:
// EmployeeTest.cpp
#include <iostream>
#include "Employee.h"
using namespace std;
using namespace Records;
int main()
{
cout << "Testing the Employee class." << endl;
Employee emp;
emp.setFirstName("Marni");
emp.setLastName("Kleper");
emp.setEmployeeNumber(71);
emp.setSalary(50000);
emp.promote();
emp.promote(50);
emp.hire();
emp.display();
return 0;
}



The declarations:
// Employee.h
#include <string>
namespace Records {
const int kDefaultStartingSalary = 30000;
class Employee
{
public:
Employee();
void promote(int inRaiseAmount = 1000);
void demote(int inDemeritAmount = 1000);
void hire(); // Hires or rehires the employee
void fire(); // Dismisses the employee
void display() const; // Outputs employee info to console
// Accessors and setters
void setFirstName(std::string inFirstName);
std::string getFirstName() const;
void setLastName(std::string inLastName);
std::string getLastName() const;
void setEmployeeNumber(int inEmployeeNumber);
int getEmployeeNumber() const;
void setSalary(int inNewSalary);
int getSalary() const;
bool getIsHired() const;
protected:
std::string mFirstName;
std::string mLastName;
int mEmployeeNumber;
int mSalary;
bool bHired;
};
}


The functions:
// Employee.cpp
#include <iostream>
#include "Employee.h"
using namespace std;
namespace Records {
Employee::Employee()
: mFirstName("")
, mLastName("")
, mEmployeeNumber(-1)
, mSalary(kDefaultStartingSalary)
, bHired(false)
{
}
void Employee::promote(int inRaiseAmount)
{
setSalary(getSalary() + inRaiseAmount);
}
void Employee::demote(int inDemeritAmount)
{
setSalary(getSalary() - inDemeritAmount);
}
void Employee::hire()
{
bHired = true;
}
void Employee::fire()
{
bHired = false;
}
void Employee::display() const
{
cout << "Employee: " << getLastName() << ", " << getFirstName() << endl;
cout << "-------------------------" << endl;
cout << (bHired ? "Current Employee" : "Former Employee") << endl;
cout << "Employee Number: " << getEmployeeNumber() << endl;
cout << "Salary: $" << getSalary() << endl;
cout << endl;
}

// Accessors and Setters
void Employee::setFirstName(string inFirstName)
{
mFirstName = inFirstName;
}
string Employee::getFirstName() const
{
return mFirstName;
}
void Employee::setLastName(string inLastName)
{
mLastName = inLastName;
}
string Employee::getLastName() const
{
return mLastName;
}
void Employee::setEmployeeNumber(int inEmployeeNumber)
{
mEmployeeNumber = inEmployeeNumber;
}
int Employee::getEmployeeNumber() const
{
return mEmployeeNumber;
}
void Employee::setSalary(int inSalary)
{
mSalary = inSalary;
}
int Employee::getSalary() const
{
return mSalary;
}
bool Employee::getIsHired() const
{
return bHired;
}
}
Please put code tags around your code [code] [/code]. It makes it much easier to read your code.
what is the error type you are getting .
"error: in C++98 'myVector' must be initialized by constructor, not by '(...)'".


but I don't see myVector in any of the given code.
I'm sorry, I forgot that I gave up on the vector problem and started working on something else. Here is the code for the vector problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
     vector<string> myVector = {"A first string", "A second string"};
     // adding some vectors using push_back
     myVector.push_back("A third string");
     myVector.push_back("The last string in the vector");
     // iterate over the elements in the vector and print them
     for (auto iterator = myVector.cbegin();
          Iterator != myVector.cend(); ++iterator)  {
          cout << *iterator << endl;
     }
     // print the elements again using C++11 range-based for loop
     for (auto& str : myVector)
          cout << str << endl;
     return 0;
}
looks like a syntax error to me, string[n] != vector<string>
Line 7 in your code is not valid C++98 -- that's the new C++11 syntax.

Only POD structs can be init using {...} init lists in C++98

And the auto is a bit new-fangled, too.
Last edited on
And the auto is a bit new-fangled, too.


It totally is, and it's freaking awesome for making iterators. :)
Any idea how to compile these with the C++11 standard?
After fixing the typo in line 13, this program compiles with gcc 4.6.2

$ g++  -W -Wall -Wextra -pedantic -O3 -march=native -std=c++0x -o test test.cc
~ $ ./test
A first string
A second string
A third string
The last string in the vector
A first string
A second string
A third string
The last string in the vector


Topic archived. No new replies allowed.