我不断收到以下错误:
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 阅读器函数使用节点[0] = id 0 name 等信息填充两个向量节点和边
尚未包含编写新 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;
};
我的列表.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
用户.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