简体   繁体   English

ISO C ++禁止声明无类型的“向量”

[英]ISO C++ forbids declaration of ‘vector’ with no type

when i compile my project i'm getting that weird errors for tools.h/tools.h. 当我编译我的项目时,我得到了tools.h / tools.h的奇怪错误。 The string and vector classes are used via std namespace. 字符串和向量类通过std名称空间使用。 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 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? 你呢?

In your file "struct.cpp", you need to #include <string> as well as #include <vector> . 在文件“ struct.cpp”中,您需要#include <string>以及#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. 当然,由于使用“ tools.h”的所有内容都需要这些,因此您可能希望将它们放在“ tools.h”的顶部,以便可以在需要的任何位置包含“ tools.h”。

添加到标题:

#include <vector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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