Auly
Auly

Reputation: 9

friend function was not declared in this scope

ListNode.h

class ListNode{
public:
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *p) : val(x), next(p) {} 
    ~ListNode() {}

    void print();      
    friend ListNode* vectorToListNode(vector<int>& nums); 
};

ListNode.cpp

#include "ListNode.h"
ListNode* vectorToListNode(vector<int>& nums)
{
    ListNode* dummy=new ListNode(0);
    ListNode* p=dummy;    
    for (int num:nums) 
    {
        p->next = new ListNode(num);
        p = p->next;
    }
    p->next=nullptr;
    p=dummy->next;
    delete dummy;
    return p;
}

main.cpp

#include "ListNode.h"
int main()
{
    vector<int> nums={1,2,3};
    ListNode* head=vectorToListNode(nums);
    head->print();
    return 0;
}

I get a error:'vectorToListNode' was not declared in this scope in VScode. Why, How to fix it when vectorToListNode is still a friend fuction.

Upvotes: 0

Views: 42

Answers (0)

Related Questions