【发布时间】:2015-11-25 19:32:13
【问题描述】:
我正在尝试编写一个控制台应用程序,例如 Twitter。 User 和 UserList 类包括彼此。我正在尝试访问以下用户的关注者。 UserList 类用于链表。
//User.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class UserList;
class User
{
friend class UserList;
private:
string userName;
string personalComment;
UserList *followed;
UserList *following;
int followedNumber;
int followingNumber;
//TWEET
UserList *blocked;
User* next;
public:
User();
User(string&,string&);
};
//UserList.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class User;
class UserList{
private:
User *root;
public:
UserList();
~UserList();
void addUser(string&,string&);
bool checkUser(string&);
User& findUser(string&);
void printList();
};
首先,我写了一个函数来查找关注用户。
//userList.cpp
User& UserList::findUser(string& name)
{
User *temp=root;
while(temp!=NULL)
{
if(!name.compare(temp->userName))
{
return temp;
}
temp= temp->next;
}
temp= temp->next;
return temp;
}
例如 user1 想关注 user2。我想检查 user1 是否已经关注 user2。( checkUser(username) 在列表中查找用户并返回 bool)
//main.cpp in main()
if(users.findUser(user1name).following->checkUser(user2name))
{
cout<<"Err: The user '"<< user1name << "' has already followed '"<<user2name<<"'!"<<endl;
}
但存在“UserList* User::following is private”错误和“在此上下文中”
如何访问该用户的列表?
【问题讨论】:
-
twitter 是一个控制台应用程序?
-
@tobi303 想的完全一样。 :P 显然有些人不使用 HTML 渲染器,而只是阅读标签和所有内容......
-
您的代码中没有
User::followed -
这里没有看到任何嵌套类,
class User里面没有类声明或定义。 -
@MervePia
UserList在哪里嵌套在User中?
标签: c++ linked-list singly-linked-list nested-class