C++ 没有用于调用错误的匹配函数。将字符串推入向量

C++ 没有用于调用错误的匹配函数。将字符串推入向量,c++,C++,获取错误: proj08-struct.cpp:在构造函数“Image::Image(std:_cxx11::string)”中: proj08 struct.cpp:41:45:错误:调用“std::vector>::push_back(\uu gnu\u cxx::\uu alloc\u traits,char>::value\u type&)时没有匹配的函数 v.向后推(第[j]行)//将行添加到向量 Image::Image (string f_name){ ifstream o

获取错误:

proj08-struct.cpp:在构造函数“Image::Image(std:_cxx11::string)”中: proj08 struct.cpp:41:45:错误:调用“std::vector>::push_back(\uu gnu\u cxx::\uu alloc\u traits,char>::value\u type&)时没有匹配的函数 v.向后推(第[j]行)//将行添加到向量

 Image::Image (string f_name){
   ifstream objfile(f_name);//creates object file
   string line;//each line/row in matrix 
   vector<vector<long>> v_;
   istringstream iss;
   long height_,width_,max_val_;
    int counter=0;
   do{
       getline(objfile,line);//goes through each line in objfile
   }//of do
       while(line[0]!='#'||line[0]!='P');//skip when the line starts with a # or P
            if(counter==0){
                iss>>height_>>width_;
                counter++;
            }//of if
            else if(counter==1){
                iss>>max_val_;
                counter++;
            }//of first else if
            else if(counter<1){
                for(int i=0; i<height_; i++){//goes to next row
                    for(int j=0; j<width_; j++){//goes through row
                        v_.push_back(line[j]);//adds row to vector
                    counter++;
                    }//of inside for 
                }//outside for
            }//of second else if

    //cout<<v_<<endl;
}//of Image contructor
Image::Image(字符串f_name){
ifstream objfile(f_name);//创建对象文件
字符串行;//矩阵中的每一行
向量v_;
istringstream;
长高度、宽度、最大值;
int计数器=0;
做{
getline(objfile,line);//遍历objfile中的每一行
}//当然
while(第[0]行!='#'| |第[0]行!='P');//当该行以#或P开头时跳过
如果(计数器==0){
iss>>高度>>宽度;
计数器++;
}//如果
else if(计数器==1){
iss>>最大值;
计数器++;
}//首先,如果

else if(counter您的向量声明是错误的。您正在声明
vector v_
并试图在
v_
中插入字符串

vector v_u
这意味着v_u是一个在v_u上的每个索引位置都有
vector
类型的向量。如果要将文本逐行存储在向量中,则需要将v_u声明更改为
vector v_u


我希望这有帮助。

您的向量声明是错误的。您正在声明
vector v\u
并试图将字符串插入
v\u

vector v_u
这意味着v_u是一个在v_u上的每个索引位置都有
vector
类型的向量。如果要将文本逐行存储在向量中,则需要将v_u声明更改为
vector v_u


我希望这会有所帮助。

是的,您需要找到一种方法,将行中的每个字符推送到一个表示行中数据的向量,然后将该向量推送到另一个表示整个文件中数据的向量。可以这样实现:

Image::Image (string f_name)
{
    ifstream objfile(f_name);
    string line;
    vector<vector<long>> v_;
    strstream iss;
    long height_, width_;
    int counter = 0;

    while( !objfile.eof() ) {
        string line;
        string junk;
        getline(objfile, line);
        if (line[0] != '#' && line[0] != 'P' && line[0]) {
            strstream iss;
            iss << line;
            if (counter == 0)
            {
                iss >> width_ >> height_;
            }
            else {
                vector<long> l;
                for (int i = 0; i < line.size(); i++)
                {
                    long temp = 0;
                    iss >> temp;    
                    l.push_back(temp);
                }
                v_.push_back(l);
            }
            counter++;
        }
    }
    for (int i = 0; i < height_; i++)
    {
        for (int j = 0; j < width_; j++)
        {
            std::cout << v_.at(i).at(j) << " ";
        }
        std::cout << std::endl;
    }
}
Image::Image(字符串f_name)
{
ifstream objfile(f_名称);
弦线;
向量v_;
stream-iss;
长高、宽;
int计数器=0;
而(!objfile.eof()){
弦线;
线绳垃圾;
getline(objfile,line);
如果(第[0]行!='#'&第[0]行!='P'和第[0]行){
stream-iss;
iss>宽度>高度;
}
否则{
向量l;
对于(int i=0;i>温度;
l、 推回(温度);
}
v.向后推(l);
}
计数器++;
}
}
对于(int i=0;istd::cout是的,您需要找到一种方法,将行中的每个字符推送到一个表示行中数据的向量,然后将该向量推送到另一个表示整个文件中数据的向量。可以这样实现:

Image::Image (string f_name)
{
    ifstream objfile(f_name);
    string line;
    vector<vector<long>> v_;
    strstream iss;
    long height_, width_;
    int counter = 0;

    while( !objfile.eof() ) {
        string line;
        string junk;
        getline(objfile, line);
        if (line[0] != '#' && line[0] != 'P' && line[0]) {
            strstream iss;
            iss << line;
            if (counter == 0)
            {
                iss >> width_ >> height_;
            }
            else {
                vector<long> l;
                for (int i = 0; i < line.size(); i++)
                {
                    long temp = 0;
                    iss >> temp;    
                    l.push_back(temp);
                }
                v_.push_back(l);
            }
            counter++;
        }
    }
    for (int i = 0; i < height_; i++)
    {
        for (int j = 0; j < width_; j++)
        {
            std::cout << v_.at(i).at(j) << " ";
        }
        std::cout << std::endl;
    }
}
Image::Image(字符串f_name)
{
ifstream objfile(f_名称);
弦线;
向量v_;
stream-iss;
长高、宽;
int计数器=0;
而(!objfile.eof()){
弦线;
线绳垃圾;
getline(objfile,line);
如果(第[0]行!='#'&第[0]行!='P'和第[0]行){
stream-iss;
iss>宽度>高度;
}
否则{
向量l;
对于(int i=0;i>温度;
l、 推回(温度);
}
v.向后推(l);
}
计数器++;
}
}
对于(int i=0;istd::cout看起来您已经将向量定义为long向量,并尝试将字符串插入向量。谢谢!我如何将数字从行推到向量向量中?我应该在我的项目中使用该类型,这就是为什么我保留它,但现在感觉卡住了。如果要求您仅从每行提取整数值,我建议您这样做逐字阅读行,检查单词是否为数值,然后检查单词是否为数值,然后将其插入向量。看起来您已将向量定义为长向量,并尝试将字符串插入向量。谢谢!我如何将数字从行推送到向量向量中?我应该在我的项目中使用该类型,这就是为什么我保留了它,但现在感觉卡住了。如果要求您只从每行提取整数值,我建议您逐字阅读这行,检查单词是否为数值,然后检查单词是否为数值,然后将其插入向量中。