Home > Enterprise >  Vector of Pointers to Structure
Vector of Pointers to Structure

Time:11-16

Please help where I am getting it wrong below. I want to maintain multiple linked list through vectors. An entry in a vector should store the pointer of the first node of a doubly linked list. Below is my code. I have 2 functions first is to insert a node in a given linked list. The header to this linked list will be stored in the vector.

typedef long long  ll;
struct block
{
  ll tag;
  bool valid_bit;
  bool dirty_bit;
  block* next;
  block* prev;
};

vector < block* > set;

// Function to Insert new block in the set
void insert(set cashblock, ll tag1=-1,bool d=0) 
{
   temp=cashblock;
   int i=0;
   //check the associativity for the set
   while (temp->prev!=NULL)
   {
......more code here....

}

bool find(set tmp,ll tag1)
{
    while(tmp->prev!=NULL)
    {
...more code here...
}

The errors are as follows-

43 | void insert(set cashblock,ll tag1=-1,bool d=0)
      |             ^~~
main.cpp:43:16: error: expected ‘)’ before ‘cashblock’
   43 | void insert(set cashblock,ll tag1=-1,bool d=0)
      |            ~   ^~~~~~~~~~
      |                )
main.cpp:43:30: error: expected primary-expression before ‘tag1’
   43 | void insert(set cashblock,ll tag1=-1,bool d=0)
      |                              ^~~~
main.cpp:43:38: error: expected primary-expression before ‘bool43 | void insert(set cashblock,ll tag1=-1,bool d=0)
      |                                      ^~~~
main.cpp:92:16: error: ‘tmp’ was not declared in this scope; did you mean ‘tp’?
   92 | bool find(set* tmp,ll tag1)
      |                ^~~
      |                tp

One more question: Also, let me know if I need to access the 0th element from the vector which is a pointer to a linked list. Should it be like set[0]->tag or what is the right way to call.

CodePudding user response:

vector < block* > set;

This creates a set variable, and its type is vector < block* >, but you're trying to use set like a type:

If you want to create a type alias for set, try:

using set = std::vector < block* > 

But I don't know why you don't use the standard std::set template

CodePudding user response:

"I want to maintain multiple linked list through vectors."

No, this is not the way to do it.


vector < block* > set;

This is a variable called set.

void insert(set cashblock, ll tag1=-1,bool d=0) 

This is a function that accepts a type named set as param.

To simply fix your errors:

#include <vector>


typedef long long  ll;
struct block
{
  ll tag;
  bool valid_bit;
  bool dirty_bit;
  block* next;
  block* prev;
};

using set = std::vector< block* > ; //very bad name, imagine using namespace std, the confusion...
set my_set;

// Function to Insert new block in the set
void insert(set cashblock, ll tag1=-1,bool d=0) { ... }

bool find(set tmp,ll tag1) { ... }

I need to access the 0th element from the vector which is a pointer to a linked list. Should it be like set[0]->tag

To access the 1st element from the vector which is a pointer to a linked list which elements are pointers to a struct*, yes you can set[0]->tag

  • Related