简体   繁体   中英

Why am I getting this error in header file “expected initializer before 'vector'”

I get this error when I am compiling the project with:

g++ -I./src -MMD -MP -std=c++11 -c -o src/scape.o src/scape.cpp

One of the files that is included in scape.cpp is tools.hpp and it is in this file that I get the error:

src/tools.hpp:4:2: error: expected initializer before 'vector'

Here follows the tools.hpp code:

#ifndef TOOLSHPP
    #define TOOLSHPP

    vector<int> decToBinary (unsigned long long int , int );
    int countFreq (string , string);
    vector<string> split_string (string ); 
    void remove_if_in_list (vector<int>& , vector<int> );
    vector<int> union_of_vectors (vector<int>, vector<int>); 
    void remove_file (string );
    void call_delay();

#endif 

So I'm expetcing that the error is due to something I am missing before the 'vector' but I really don't see what is it?

EDITED

After trying some ideas I also figured out that the error is not only related to the 'vector'. If I change lines in the above code resulting in the code bellow, the error changes to:

src/tools.hpp:7:2: error: expected initializer before 'int'

#ifndef TOOLSHPP
    #define TOOLSHPP

    #include <string>
    #include <vector>

    int countFreq (std::string, std::string);
    std::vector<int> decToBinary (unsigned long long int, int);
    std::vector<std::string> split_string (std::string); 
    void remove_if_in_list (std::vector<int>&, std::vector<int>);
    std::vector<int> union_of_vectors (std::vector<int>, std::vector<int>); 
    void remove_file (std::string);
    void call_delay();

#endif

Thanks for the help.

The erros was actually in another file "pos_processing.hpp" (below):

#ifndef POSPROCESSINGHPP
    #define POSPROCESSINGHPP

    #include "types.hpp"

    void update_report (MyGraphType, vector<int> , int , int , map<int,vector<errorProbabilityType>>& );
    void generate_report (string , string , map< int, vector<errorProbabilityType> > )

#endif

There is a missing semicolon in the last function declaration. This header file was included together with other headers, as presented bellow.

#include "scape.hpp"
#include "exprtk.hpp"
#include "types.hpp"
#include "debug.hpp"
#include "pre_processing.hpp"
#include "pos_processing.hpp"
#include "tools.hpp"
#include "scape_utils.hpp"

As the error is a missing semicolon in the last line of the file, the compiler understands that the next line (which is in the next include file) is part of the previous one, and ends up indicating the error in the next line, which is in the "tools.hpp" file. That is why the error was in one file but was being indicated at the other.

It seems that your tools.hpp file does not use #include <vector> and std namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM