C++ 将变量类型转换为*类型的函数创建错误C++;
我不断得到一个错误:C++ 将变量类型转换为*类型的函数创建错误C++;,c++,list,class,pointers,C++,List,Class,Pointers,我不断得到一个错误: 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’
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
在user.h:3:0中包含的文件中,
从序号cpp:5:
'mylist.h:在成员函数'void mylist::push_back(L)[with L=int]'中:
user.h:38:30:从此处实例化
mylist.h:54:3:错误:从“int”到“int*”的转换无效[-fpermissive]
h:在成员函数“void mylist::push_back(L)[with L=User*]”中:
序号cpp:61:25:从此处实例化
mylist.h:54:3:错误:无法将分配中的“用户*”转换为“用户**”
制造:**[sn.o]错误1
我正在创建一个基本的社交网络,其中main接受3个命令行参数-argv[1]是一个GML文件,其中的节点包含用户信息和作为用户连接的边。argv[2]是我尚未处理的另一个文件。argv[3]是一个GML文件,它基本上包含用户信息的一个副本,经过解析并放入我编写的ADT列表MyList中,其中包含user*的实例,其中包含用户id、姓名、邮政编码和年龄的私有数据。出于某种原因,我的pushback函数将另一项添加到我的列表中,这会使指针变为双指针或非指针指针,从而产生上述错误。我只是不知道我需要在哪里删除*或者我做错了什么。GML reader函数使用以下信息填充两个向量节点和边:
节点[0]=id 0名称“Mark Redekopp”年龄34 zip 90018
节点[1]=id 1名称“Tommy特洛伊木马”年龄124 zip 90007
及
边[0]=源0目标1
边[1]=源1目标0
编写新GML文件的代码还没有包括在内
序列号文件
#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;
};
#包括
#包括
#包括
#包括
#包括“user.h”
#包括“mylist.h”
#包括“gmlreader.h”
使用名称空间std;
int main(int argc,char*argv[]){
如果(argc<4){
cerr问题在于推回,具体来说,这一行:
data_=newVal;
newVal
是一个L
,但是data
是一个L*
。我想你的意思是data=templast
不过,不要忘记删除数据的旧值。您的问题和代码都难以辨认。代码太多了,无法缩小范围。啊,您的代码太紧凑了……您怎么能不流泪地阅读它呢?为什么不使用一些已经调试好且文档完整的容器类呢这是用C++标准库还是Boost的指针容器来进行的,也调试过并记录了(什么是代码54?MyList.H./Cuth>?):下次当你用“MyList.H:54∶3”和整个文件的位置发布错误时,至少提供行号。没有人喜欢计算它。或者像建议的那样缩短它。
#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
data_=newVal;