-3

hey guys i have created a struct node. one of its fields is a vector (path) where i want to store characters.however when i try to push_back a character the compiler says "error: ‘path’ was not declared in this scope"

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <list>
#include <climits>
using namespace std;

struct node {
    int weight;
    bool pizza;   // true an tin exo
    vector <char> path;
    int tetmimeni, tetagmeni; // i, j gia na vro geitones
    } ;
node a;

int main(){
 a.tetmimeni=0;   // create start node
 a.tetagmeni=0;
 a.weight=0;
 a.pizza=true;
 a.path= path.push_back('S');

2 Answers2

2

Replace a.path= path.push_back('S'); with just a.path.push_back('S');

The original code was trying to assign the return type of push_back to a.path which is invalid.

Instead you simply want to invoke the push_back method of the std::vector member of your struct.

const_ref
  • 4,016
  • 3
  • 23
  • 38
0

In your code , node is a structure. Path is one element of struct. Anytime you need to access element of struct , you have to use the name of struct along with it.

e.g. a.pizza or a.weight when 'a' is of the type node.

Similarly you need to access a.path when you want to access vector path. It doesn't matter even if you need to call the functions of vector.

You should go through struct/class

Graham
  • 7,431
  • 18
  • 59
  • 84