函数将变量Type转换为Type*,导致C++错误

Function converting variable Type to Type* creating error C++

本文关键字:Type 导致 C++ 错误 变量 转换 函数      更新时间:2023-10-16

我一直收到一个错误:

In file included from user.h:3:0,
                from sn.cpp:5:
'mylist.h: In member function ‘void MyList<L>::push_back(L) [with L = int]’:
user.h:38:30:   instantiated from here
mylist.h:54:3: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
mylist.h: In member function ‘void MyList<L>::push_back(L) [with L = User*]’:
sn.cpp:61:25:   instantiated from here
mylist.h:54:3: error: cannot convert ‘User*’ to ‘User**’ in assignment
make: *** [sn.o] Error 1

我正在创建一个基本的社交网络,其中main使用3个命令行参数-argv[1]是一个GML文件,其节点包含用户信息和用户连接的边。argv[2]是我尚未处理的另一个文件。argv[3]是一个GML文件,在解析并放入我编写的ADT列表MyList后,它基本上会包含用户信息的副本,其中包含user*的实例,这些实例包含用户id、姓名、邮政编码和年龄的私人数据。出于某种原因,我向列表中添加另一项的推回函数要么使指针成为双指针,要么使指针变成非指针,这会导致上面的错误。我只是不知道我需要在哪里删除一个*,也不知道我做错了什么。GML读取器函数用诸如nodes[0]=id 0 name"Mark Redekopp"age 34 zip 90018nodes[1]=id 1 name"Tommy Trojan"age 124 zip 90007和edges[0]=源0目标1edges[1]=源1目标0

写入新GML文件的代码尚未包含

sn文件

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "user.h"
#include "mylist.h"
#include "gmlreader.h"
using namespace std;
int main(int argc, char* argv[]){
  if(argc < 4){
    cerr << "Please provide the input GML file, command file, and output file" << endl;
    return 1;
  }
vector<string>nodes;
vector<string>edges;
GMLReader::read(argv[1], nodes, edges); 
for(unsigned int i =0; i<nodes.size(); i++){
    cout << "node[" << i << "]: " << nodes[i] << endl;
};
for(unsigned int i=0; i<edges.size(); i++){
    cout << "edge[" << i << "]: " << edges[i] << endl;
    cout << "printing an edge!" << endl;
};
cout << "about to create a mylist of users" << endl;
MyList<User*>Users;
cout << "initialized user list" << endl;
for(unsigned int i =0; i<nodes.size(); i++){
    string TextBlob = nodes[i];
stringstream ss(TextBlob);
cout << "started string stream" << endl;
User* newuser = new User; 
    while(newuser->getName()=="" || newuser->getId()==0 || newuser->getZip()==0||        newuser->getAge()==0){  
    if (TextBlob.compare("name")==0){
        string n;
        ss>>n;
        newuser->setName(n);
    }
    else if(TextBlob.compare("age")==0){
        int a;
        ss>>a;
        newuser->setAge(a);
    }
    else if(TextBlob.compare("id")==0){
        int d;
        ss>>d;
        newuser->setId(d);
    }
    else if(TextBlob.compare("zip")==0){
        int z;
        ss>>z;
        newuser->setZip(z);
    }
}
Users.push_back(newuser);
}
return 0;
};

mylist.h

#include <iostream>
 #include <string>
 #include <vector>
 #include <stdexcept>
 #ifndef MYLIST_H
 #define MYLIST_H
 using namespace std;
 template<typename L>
 class MyList{
private:
    L* data_;
    int len_;
    int MAX_LIST_SIZE;
public:
    MyList();
    ~MyList();
    void push_back(L newVal);
    int size();
    L& at(int loc); 
    bool remove(L val);
    L pop(int loc);
    L& operator[](int loc);
    void changeLen(int new_len){
        len_=new_len;
    }
 };
 int MAX_LIST_SIZE=100;
 template<typename L>
 MyList<L>::MyList(){
        data_ = new L[MAX_LIST_SIZE];
        len_=0;
    };
 template<typename L>
 MyList<L>::~MyList(){
        delete [] data_;
    };
 template<typename L>
 void MyList<L>::push_back(L newVal){
if(len_==MAX_LIST_SIZE-1){
    L* tempList = new L[MAX_LIST_SIZE*2];
    for(int i=0; i<len_; i++){
        tempList[i]=data_[i];
        MAX_LIST_SIZE*=2;
        }
    tempList[len_++]=newVal;
    data_=newVal;
}
data_[len_++]=newVal;
    };
 template<typename L>
 int MyList<L>::size(){
        return len_;
    };
 template<typename L>
 L& MyList<L>::at(int loc){
    if(loc > len_)
        throw invalid_argument("Out of bounds");
return  data_[loc];
};      
 template<typename L>
 bool MyList<L>::remove(L val){
for(int i=0; i<len_; i++){
    if(data_[i]==val){
        for(int j=i; j<len_-1; j++){
            data_[j]=data_[j+1];
        }
        changeLen(len_-1);
        return true;
    };
};
return false;
 };
 template<typename L>
 L MyList<L>::pop(int loc){
if(loc>len_)
    throw invalid_argument("Out of bounds");
L temp;
data_[loc] = temp;
    for(int i=len_; i>=loc; i--){
        data_[i-1]=data_[i];
};
changeLen(len_-1);
return temp;
 };
 template<typename L>
 L& MyList<L>::operator[](int loc){
return  data_[loc];
};
 #endif

user.h

#ifndef USER_H
#define USER_H
#include "mylist.h"
class User{
public: 
    User(){
        name_=""; age_ =0; zip_=0; id_=0;};
    ~User();
    void setName(string name){
        name_=name;
    };
    string getName(){
        return name_;
    };
    void setAge(int age){
        age_=age;
    };
    int getAge(){
        return age_;
    };
    void setId(int id){
        id_=id;
    };
    int getId(){
        return id_;
    };
    void setZip(int zip){
        zip_=zip;
    };
    int getZip(){
        return zip_;
    };
    MyList<int> getFriends(){
        return Friends;
    };
    void addFriend(int friendid){
        Friends.push_back(friendid);
    };
    void printUser(){
        cout<< "User Name: " << name_ << endl;
        cout<< "User Age: " << age_ << endl;
        cout<< "User's Friends: ";
            for(int j=0; j<Friends.size(); j++){
                cout <<Friends.at(j) << " ";
        };
        cout << endl;
    };
private:
    string name_;
    int age_;
    int id_;
    int zip_;
    MyList<int> Friends;
};

#endif

问题在push_back中,具体地说,这一行:

data_=newVal;

newValL,但data_L*。我认为你的意思是说data_ = tempList

不过,不要忘记删除data_的旧值。