error: invalid use of incomplete type ‘struct Queue’

I'm getting a lot of errors on this code and I don't really know what this one means. Yes my code is probably all over the place but just bare with me. Could anyone tell me why this error is happening?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
using namespace std;

class Queue;

class City {
  friend class Queue;
  friend class DataOrganizer;
  Queue *q;       // List of cities this one is directly connected to
  City  *from;    // To trace a path back to origin
  string name;    // Name of the city
  bool   mark;    // false=unmarked, true=visiting or done
  City *nextOne;

public:
  City () { q = new Queue();  from = NULL;  mark = false;  name = ""; nextOne = NULL; }
  void setName (string name);
  void neighbor (City *c) { q->enqueue(c); }
  City *next () { return (City *)q->dequeue(); }
  void setMark() { mark = true; }
  void setFrom(City* g) { from = g; }
  void displayBestRoute () {     // displays route back to city of origin
    cout << name << " ";
    while (from != NULL) {
      cout << from->name << " ";
      from = from;
    }
  }

  ~City () { delete q; if (name != NULL) delete name; }
};

class Queue {
  friend class City; 
  City *tail;
public:
  Queue() { tail = NULL;  }

  void enqueue(City *t) {   //might need to change to City??
    if (t == NULL) return;
    if (tail == NULL) {
      tail = new City();
      tail->setName(t->name);
      tail->nextOne = tail; }
    else {
      City *h = new City();
      h->setName(t->name);
      h->nextOne = tail->nextOne;
      tail->nextOne = h;
      tail = h;
    }
  }

  City *dequeue() {
    if (tail == NULL) return NULL;
    City *ptr = tail->nextOne;
    if (ptr != tail) tail->nextOne = ptr->nextOne; 
    else tail = NULL;
    City *t = ptr;
    delete ptr;
    return t; 
  }

  int isEmpty() {  return tail == NULL;  }
};

class DataOrganizer {
  int count;      // Counts the number of cities
  string name;    // accepts input from the file
  City **cities;

 public:
  DataOrganizer () { count = 0; }
  
  void readInput (char *filename) {
    fstream *fin = new fstream(filename, ios::in);
    while (true) {
      string buffer;
      *fin >> buffer; 
      if (buffer.compare("-") == 0) break;
      count ++; }
    fin->close();  
    fin->clear();
    
    string tok;
    cities = new City*[count];
    fin->open(filename, ios::in);
    size_t mark = fin->tellg(); 
    for (int i=0; *fin>>tok && tok != "-"; i++) cities[i]->setName(tok);
    for (int i=0; i < count; i++) {
      for (int j=0; *fin>>tok && tok != "-"; j++) { 
	int n;
	istringstream(tok) >> n;
	cities[i]->neighbor(cities[n]); 
      }  
    }
    fin->close();
    fin->clear();
  }

  City *getOrigin() { return cities[0]; }
  City *getDestination() { return cities[count]; }
};

int main (int argc, char **argv) {
  if (argc != 2) {
    cout << "Usage: " << argv[0] << " \n";
    exit(0);
  }

  DataOrganizer dao;
  dao.readInput(argv[1]);

  // Set origin and destination cities
  City *origin = dao.getOrigin();
  City *destin = dao.getDestination();
  City *city;
  Queue *q = new Queue ();  // Simulate simultaneity

  origin->setMark();
  q->enqueue(origin);
  while (!q->isEmpty()) {
    City *current = (City*) q->dequeue();
    while((city = current->next()) != NULL) {
      if (city == destin) {
	city->setFrom(current);
	cout << "Origin: " << origin << endl;
	cout << "Destin: " << destin << endl;
	city->displayBestRoute();
	return 0;
      }
      if (!(city->mark == true)) {
	city->setMark();
	city->setFrom(current);
	q->enqueue(city);
      }
    }
  }
  cout << "No Route Between Selected Cities\n";
}
Last edited on
You should show error messages and there they occur in the code.

I think this statement

City () { q = new Queue(); from = NULL; mark = false; name = ""; nextOne = NULL; }

is invalid because the compiler does not know the definition of class Queue.
Last edited on
Well what should I do about it? Most of the code was given to my class by the professor and we had to go off what he gave us and that part was already there soooo. Idk what id have to do lol

The Errors:

homework6.cc: In constructor ‘City::City()’:
homework6.cc:25:27: error: invalid use of incomplete type ‘struct Queue’
homework6.cc:13:7: error: forward declaration of ‘struct Queue’
homework6.cc: In member function ‘void City::neighbor(City*)’:
homework6.cc:27:30: error: invalid use of incomplete type ‘struct Queue’
homework6.cc:13:7: error: forward declaration of ‘struct Queue’
homework6.cc: In member function ‘City* City::next()’:
homework6.cc:28:35: error: invalid use of incomplete type ‘struct Queue’
homework6.cc:13:7: error: forward declaration of ‘struct Queue’
homework6.cc: In destructor ‘City::~City()’:
homework6.cc:39:21: warning: possible problem detected in invocation of delete operator: [enabled by default]
homework6.cc:39:21: warning: invalid use of incomplete type ‘struct Queue’ [enabled by default]
homework6.cc:13:7: warning: forward declaration of ‘struct Queue’ [enabled by default]
homework6.cc:39:21: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
homework6.cc:39:36: error: no match for ‘operator!=’ in ‘((City*)this)->City::name != 0’
homework6.cc:39:36: note: candidates are:
/usr/include/c++/4.6/bits/postypes.h:223:5: note: template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/stl_pair.h:214:5: note: template<class _T1, class _T2> bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:297:5: note: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:347:5: note: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/allocator.h:132:5: note: template<class _T1, class _T2> bool std::operator!=(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/include/c++/4.6/bits/allocator.h:137:5: note: template<class _Tp> bool std::operator!=(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/include/c++/4.6/bits/basic_string.h:2473:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2485:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2497:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/streambuf_iterator.h:200:5: note: template<class _CharT, class _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
/usr/include/c++/4.6/ext/new_allocator.h:128:5: note: template<class _Tp> bool __gnu_cxx::operator!=(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
/usr/include/c++/4.6/bits/stl_iterator.h:817:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator!=(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
/usr/include/c++/4.6/bits/stl_iterator.h:811:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator!=(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
homework6.cc:39:49: error: type ‘std::string {aka struct std::basic_string<char>}’ argument given to ‘delete’, expected pointer
homework6.cc: In function ‘int main(int, char**)’:
homework6.cc:21:10: error: ‘bool City::mark’ is private
homework6.cc:159:19: error: within this context
The compiler shall know the definition of class Queue when you use operator new for it.
operator new?
Is anyone gonna help me out?
Are you capable to read what I have written?!

City () { q = new Queue(); from = NULL; mark = false; name = ""; nextOne = NULL; }
Yes I can read it. I already have that in my code? What are you trying to tell me here?
It is awully! I already wrote


The compiler shall know the definition of class Queue when you use operator new for it.


How many times shall it be repeated?!
Topic archived. No new replies allowed.