Home > Software engineering >  Data cannot show // cant check if [duplicate]
Data cannot show // cant check if [duplicate]

Time:09-27

/so i couldn't show the data 'head' in SLinkedList.cpp cant check if and cant add node because when i use the head variable it error/

*Compilation results... C is a general-purpose programming language. It was originally designed as an extension to C and has a similar syntax, but it is now a completely different language. Use this tag for questions about code (to be) compiled with a C compiler. Use a version-specific tag for questions related to a specific standard revision Errors: 0 Warnings: 0

Output

SLinkedList.h

#ifndef SLINKEDLIST_H_INCLUDED
#define SLINKEDLIST_H_INCLUDED

using namespace std;
class Account;

class SLinkedList{
public:
    struct Node{
        Node* next;
        Account* account;
        Node(Account* acc){
            account = acc;
            next = NULL;
        }
    };
    

    SLinkedList();
    ~SLinkedList();
    void add(Account*);
    void addToTail(Account*);
    void removeFromHead();
    int countList();
   
 
    
    Node *head;
    Node *tail;
};

#endif // SLINKEDLIST_H_INCLUDED

SLinkedList.cpp

#include <cstring>
#include <iostream>
#include "SLinkedList.h"
    SLinkedList::SLinkedList(){
        
        
     
    }
    SLinkedList::~SLinkedList(){
           cout<<"delete" ;     
    Node *temp = head ;
    while(temp !=NULL)
        {
       Node  * x = temp->next;
           delete temp ;
           temp  = x ;
        }
        
        head = NULL;
        tail = NULL;
    }
    
    void SLinkedList::add(Account*data){
        
        Node* newNode = new Node(data);
        if (head == NULL) {
            
                head = newNode;
                return;
            }
           
            // Traverse till end of list
            
            Node* temp = head;
            
            while (temp->next != NULL) {
          
                // Update temp
                temp = temp->next;
            }
          
            // Insert at the last.
            temp->next = newNode;
    }
    
    void SLinkedList::addToTail(Account*data){
        
        
             Node* newNode = new Node(data);
       
        // Assign to head
         
       /* if (  head  == NULL) {
            
            head = newNode;
            return;
        }
       
        // Traverse till end of list
        
        Node* temp = head;
        
        while (temp->next != NULL) {
      
            // Update temp
            temp = temp->next;
        }
      
        // Insert at the last.
        temp->next = newNode;
             
          */
 
        
        
    
    }
    void SLinkedList::removeFromHead(){
        
    }
    int SLinkedList::countList(){
        
        return 0;
    }

Account.h

#ifndef ACCOUNT_H_INCLUDED
#define ACCOUNT_H_INCLUDED

#include <iostream>
#include <cstring>
#include "SLinkedList.h"
class SLinkedList;

using namespace std;

class Account{
private:
    string accID;
    string name;
    string surname;
    string username;
    SLinkedList* following;
    SLinkedList* followers;

public:

    Account(string,string,string,string);
    void addFollowing(Account*);
    //void addFollowers(Account*);
    void printFollowing();
    void printFollowers();
    void printFollowing(int m);
    void printFollowers(int m);
    Account* findMostFollowingInfluencer();
    Account* findMostFollowerInfluencer();
    Account* searchFollowerByUsername(string);
    Account* searchFollowingByUsername(string);
    void appendFollowing(SLinkedList*);
    void removeAllFollowing();
    void print();

    string getAccountID();
    string getName();
    string getUsername();
    string getSurname();
    SLinkedList* getFollowingList();
};


#endif // ACCOUNT_H_INCLUDED

Account.cpp

#include <cstring>
#include <iostream>
 
#include "Account.h"
 
   Account :: Account(string accID ,string name,string surname ,string username){
        this->accID =accID;
        this->name =name;
        this->surname =surname;
        this->username =username;
         
    }
    void Account::addFollowing(Account* ac){
        cout  <<"    addFollowing"<<endl;
         
     following->add(ac);
     
            
    }
    //void addFollowers(Account*);
    void Account::printFollowing(){
        
    }
    void Account::printFollowers(){
        
    }
    void Account::printFollowing(int m){
        
    }
    void Account::printFollowers(int m){
        
    }
    Account* Account::findMostFollowingInfluencer(){
        Account* acc ;
        return  acc;
    }
    Account* Account::findMostFollowerInfluencer(){
    Account* acc ;
        return  acc;
    }
    Account* Account::searchFollowerByUsername(string){
        Account* acc ;
        return  acc;
    }
    Account* Account::searchFollowingByUsername(string){
        Account* acc ;
        return  acc;
    }
    void Account::appendFollowing(SLinkedList*){
        
    }
    void Account::removeAllFollowing(){
        
    }
    void Account::print(){
        
    }

    string Account::getAccountID(){
        return accID ;
    }
    string Account::getName(){
        return name ;
    }
    string Account::getUsername(){
        return surname ;
    }
    string Account::getSurname(){
        return username ;
    }
    SLinkedList* Account::getFollowingList(){
        SLinkedList* sl;
        return sl;
    }

main.cpp

#include <iostream>
#include "Account.h"
using namespace std;

int main()
{
    Account* a1 = new Account("5209610111", "Bingjiao", "He", "binggang");
    Account* a2 = new Account("5209680123", "Chaiwichit", "Boonsaner", "ohm123");
    Account* a3 = new Account("5509644444", "Mike", "Piromporn", "mike19");
    Account* a4 = new Account("6209601456", "Tai", "Orathai", "tai789");
    Account* a5 = new Account("6209650112", "Tzuying", "Tai", "tai19");
    Account* a6 = new Account("6309610124", "Lalisa", "M.", "lalalalisa_m");
    Account* a7 = new Account("6309621111", "Ratchanok", "Intanon", "may4432");
    Account* a8 = new Account("5907450123", "Busanan", "Ongbamrungphan", "cream123");
    Account* a9 = new Account("6104010001", "Xuerui", "Li", "lxr1991");
    Account* a10 = new Account("6104010002", "Chongwei", "Li", "lcw1987");
     
    a1->addFollowing(a5);    a1->addFollowing(a7);    a1->addFollowing(a8);    a1->addFollowing(a9);
    
    a5->addFollowing(a1);    a5->addFollowing(a7);    a5->addFollowing(a8);    a5->addFollowing(a9);
    a5->addFollowing(a3);    a5->addFollowing(a6);

    a2->addFollowing(a1);    a2->addFollowing(a7);    a2->addFollowing(a8);    a2->addFollowing(a9);
    a2->addFollowing(a3);    a2->addFollowing(a6);

    a3->addFollowing(a1);    a3->addFollowing(a7);    a3->addFollowing(a8);    a3->addFollowing(a9);
    a3->addFollowing(a6);    a3->addFollowing(a6);

    a4->addFollowing(a1);

    a8->addFollowing(a6);

    a8->printFollowing();
    a8->printFollowers();

    a8->printFollowing(2);
    a8->printFollowers(2);

    if (a8->findMostFollowingInfluencer())
        cout << "The influencer among people following " << a8->getName()
        << " is " << a8->findMostFollowingInfluencer()->getName() << endl;
    else
        cout << "There's no influencer among people following " << a8->getName() << endl;

    if (a8->findMostFollowerInfluencer())
        cout << "The influencer among people followed by " << a8->getName()
        << " is " << a8->findMostFollowerInfluencer()->getName() << endl;
    else
        cout << "There's no influencer among people followed by " << a8->getName() << endl;

    if (a10->findMostFollowerInfluencer())
        cout << "The influencer among people followed by " << a10->getName() <<
        " is " << a10->findMostFollowerInfluencer()->getName() << endl;
    else
        cout << "There's no influencer among people followed by " << a10->getName() << endl;

    if (a8->searchFollowerByUsername("mike19"))
        cout << a8->getName() << " has " << a8->searchFollowerByUsername("mike19")->getName()
        << "as his/her follower." << endl;
    else
        cout << "Username not found in the follower list." << endl;

    if (a8->searchFollowerByUsername("mike1911"))
        cout << a8->getName() << " has " << a8->searchFollowerByUsername("mike19")->getName()
        << "as his/her follower." << endl;
    else
        cout << "Username not found in the follower list." << endl;

    if (a6->searchFollowingByUsername("binggang"))
        cout << a6->getName() << " has " << a6->searchFollowerByUsername("binggang")->getName()
        << "as his/her follower." << endl;
    else
        cout << "Username not found in the following list." << endl;

    if (a5->searchFollowingByUsername("may4432"))
        cout << a5->getName() << " has " << a5->searchFollowingByUsername("may4432")->getName()
        << " as his/her follower." << endl;
    else
        cout << "Username not found in the following list." << endl;

    a4->printFollowing();
    a4->appendFollowing(a3->getFollowingList());
    a4->printFollowing();

    a4->removeAllFollowing();

    a4->printFollowing();
    return 0;
}

pls help me Can you help to show the output correctly?

CodePudding user response:

I believe the error is in a few places.

As the commentors state:

> #include <iostream>
> #include <cstring>
> 
> class SLinkedList;

You are forward declariing SLinkedList. You should however be doing a #include "SLinkedList.h". Then in your Account.h file you need to modify your constructor/deconstructor.

In your constructor, You fail to initialize the accountData member 'following'. You should add a NEW call to your constructor, and then a DELETE call to your deconstructor. However, to be more inline with modern standards, I would reccomend using smart pointers for this situation.

Account :: Account(string accID ,string name,string surname ,string username){
        this->accID =accID;
        this->name =name;
        this->surname =surname;
        this->username =username;
         
    }
    void Account::addFollowing(Account* ac){
        cout<<following->test <<"    DDDD"<<endl;
          
    }

Lastly, Since the datatype for 'test' in SList class is private, it means you can't access it directly. You need to expose the PRIVATE data member to other files by using PUBLIC getters/setters. Search around for class OOP and public/private getters setters.

  •  Tags:  
  • c
  • Related