mardi 2 mai 2017

Compile error: 'does not name a type' despite including header file and 'std::' prefix

I need help with compiling and std namespace. I am supposed to write some classes for a homework. In some of those classes I want to use C++ string data type. When I try to compile my code

// main.cpp
#ifndef __TEST__
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cassert>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include <algorithm>
using namespace std;
#endif /* __TEST_ */

class CItem
{
  protected:
    int prize;
  public:
    CItem ( int _prize )
    {
       this->prize = _prize;
    }
    std::string print ()    // this line causes error
    {
       return "Prize is " + std::to_string(this->prize) + ".\n";
    }
};     

#ifndef __TEST__
int main ( void )
{
  ... // testing system statements
}
#endif

on the school testing system I get following error: error: ‘string’ in namespace ‘std’ does not name a type.

The testing environment takes student's program, adds header files (same as in the ifndef/endif block here) and main() function with test data. Compiler settings is this:

g++ -std=c++11 -Wall -pedantic -Wno-long-long -O2 -D__TEST__ -o main main.cpp.

But when I compile it on my PC with the same flags (without -D__TEST__), no errors or warnings are displayed. I'm using Lubuntu 16.10 and g++ version 4:5.3.1-1ubuntu1. I don't know what OS and g++ version is used by the testing environment.

I'm supposed not to edit code between #ifndef __TEST__ and #endif in any way. I also can't include any other header files and I must keep all my code in one single .cpp file.

The problem is that I don't know why am I getting this compile error. I didn't have any problems with previous homeworks despite conditions being the same.

Any advice is appreciated.

Aucun commentaire:

Enregistrer un commentaire