我一直收到错误:
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,name的私有数据,邮政编码和年龄。由于某种原因,我的回滚功能是将另一个项目添加到我的列表中,要么是指针指针,要么是非指针指针,这会产生上面的错误。我只是无法弄清楚我需要删除*或我做错了什么。 GML阅读器函数使用诸如的信息填充两个向量节点和边 nodes [0] = id 0 name“Mark Redekopp”age 34 zip 90018 节点[1] = id 1名称“Tommy Trojan”年龄124邮编90007 和 edges [0] = source 0 target 1 edges [1] = source 1 target 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
答案 0 :(得分:2)
问题出在push_back
,具体来说,这一行:
data_=newVal;
newVal
是L
,但data_
是L*
。我认为意味着所说的是data_ = tempList
。
不要忘记删除data_
的旧值。