无法初始化内部类的对象C++
Can't initialize object of an inner class C++
我有两个类,一个嵌套在另一个里面:
class List {
private:
class Node {
public:
Token data;
Node* next;
Node(const Token &dataItem, Node* nextptr);
};
Node* first;
int length;
public:
List();
virtual ~List();
void insert (const Token &t);
Node* lookup(const Token &t) const;
};
类节点有它的构造函数:
List::Node::Node(const Token &dataItem, Node* nextptr): data(dataItem) {
data = dataItem;
next = nextptr;
}
但是当我尝试在插入方法中初始化一个节点时,像这样:
void List::insert (const Token &t){
if(first == NULL){
Node* node = new Node(t, nullptr); //PROGRAM DIES HERE
first = node;
cout<<"hey"<<endl;
}else{
Node* isHere = lookup(t);
//if not
if(isHere == NULL){
Node* temp = first;
Node* node = new Node(t, NULL);
while(true){
if((temp->data.getName().compare(t.getName())>0)or(temp->next==NULL)){
Node* temp2 = temp->next;
temp->next = node;
node->next = temp2;
break;
}
temp=temp->next;
}
}
//if node exists
else{
cout<<"node exists"<<endl;
isHere->data.newEntry(t.getLines()[0]);
cout<<"node out"<<endl;
}
}
}
查找方法:
List::Node* List::lookup(const Token &t) const{
if(first == NULL)
return first;
if(first->data.isEquivalent(t))
return first;
cout<<"finished coparre"<<endl;
Node* temp = first;
while(temp->next != NULL){
cout<<"in da loop"<<endl;
temp = temp->next;
if(temp->data.isEquivalent(t))
return temp;
}
return NULL;
}
程序只是在我注释的行处静默死亡。我在这里做错了什么?好的,这是代币。我不认为那里有问题,但它也被要求,所以这里是来源:令牌头:
class Token {
std::string name; // token name
int frequency;//frequency
Vector lines;//lines where the token is present
public:
//explanations for the methods in the Token.cpp
Token(std::string tokenname, int linenumber);
virtual ~Token();
void newEntry(int &linenumber);
Vector getLines() const;
std::string getName() const;
int getFrequency() const;
const bool isEquivalent(const Token &t);
;
标记方法:
Token::Token(string tokenname, int linenumber) {
// TODO Auto-generated constructor stub
name = tokenname;
frequency=1;
lines.push_back(linenumber);
}
void Token::newEntry(int &linenumber){
cout<< "enter new entry"<<endl;
frequency++;
lines.push_back(linenumber);
cout<< "finish new entry"<<endl;
}
std::string Token::getName() const{
return name;
}
const bool Token::isEquivalent(const Token &t){
if(t.getName().compare(name)==0)
return true;
else
return false;
}
Vector Token::getLines() const{
return lines;
}
构造函数出错:
List::Node::Node(const Token &dataItem, Node* nextptr): data(dataItem) {
data = dataItem;
next = nextptr;
}
我们还需要传递一个指针以使其正确工作
List::Node::Node(const Token &dataItem, Node* nextptr): data(dataItem), next(nextptr){}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 使用"std::unordereded_map"映射到"std::list"对象