ISO C++ forbids declaration of ‘vector’ with no ty

2019-07-29 23:06发布

问题:

when i compile my project i'm getting that weird errors for tools.h/tools.h. The string and vector classes are used via std namespace. I just can't see any mistakes.

g++ powerpi.cpp structs.cpp xmlreader.cpp tools.cpp -o powerpi
In file included from structs.cpp:5:
tools.h:5: error: ISO C++ forbids declaration of ‘vector’ with no type
tools.h:5: error: invalid use of ‘::’
tools.h:5: error: expected ‘;’ before ‘<’ token

tools.h

class Tools {
  public:
    template<typename T>
    static std::string convert(T);
    static std::vector<std::string> explode(std::string, std::string);
};

tools.cpp

#include <sstream>
#include <vector>
#include <string>

#include "tools.h"

template <typename T>
std::string Tools::convert(T Number)
{ 
  std::ostringstream ss;
  ss << Number;
  return ss.str();
}

std::vector<std::string> Tools::explode(std::string delimiter, std::string str)
{   
    std::vector<std::string> arr;

    int strleng = str.length();
    int delleng = delimiter.length();
    if (delleng==0)
        return arr;//no change

    int i=0;
    int k=0;
    while( i<strleng )
    {   
        int j=0;
        while (i+j<strleng && j<delleng && str[i+j]==delimiter[j])
            j++;
        if (j==delleng)//found delimiter
        {
            arr.push_back(  str.substr(k, i-k) );
            i+=delleng;
            k=i;
        }
        else
        {
            i++;
        }
    }
    arr.push_back(  str.substr(k, i-k) );
    return arr;
}

I can't see any mistakes. How about you?

回答1:

In your file "struct.cpp", you need to #include <string> as well as #include <vector>.

Of course, since everything that uses "tools.h" needs these, you may want to stick them at the top in "tools.h" instead, so that you can include "tools.h" anywhere it is needed.



回答2:

Add to your header:

#include <vector>


标签: c++ g++