Prob with vector of struct pointers!

Hello,

I am kind of stuck in a simple problem with vector of struct. I have used this many times before without any problem but suddenly some issues are now cropping up!
//test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef __TEST_H
#define __TEST_H

#include <vector>

class test{

  struct A {
    int a;
    int b;
  };
  
 public:
  vector<A*> table;
  test();
  A* getRowFromTable(int index) const;
};

#endif 


//test.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "test.h"

test::test() {
  for(int i=0; i<2; i++) {
    A* mem = new A;
    mem->a = i+10;
    mem->b = i*10;
    table.push_back(mem);
  }
}

A* test::getRowFromTable(int index) const {
  return table[index];
}


The code does not compile and I could not find a reason since I have used this type of data structures before successfully.
The error messages are:

user@localhost 200% g++ -Wall -c -O3 test.C
In file included from test.C:1:
test.h:14: error: ISO C++ forbids declaration of ‘vector’ with no type
test.h:14: error: expected ‘;’ before ‘<’ token
test.C: In constructor ‘test::test()’:
test.C:8: error: ‘table’ was not declared in this scope
test.C: At global scope:
test.C:12: error: expected constructor, destructor, or type conversion before ‘*’ token
user@localhost 201%

Why does it not able to recognize the declared struct!
Any help will be highly appreciated!

Thanks!
Last edited on
std::vector, not vector.
Isn't vector in the std namespace?

Try using namespace std; at the beginning of your code.
Last edited on
Both the above options gives the following error:

test.C:12: error: expected constructor, destructor, or type conversion before ‘*’ token
I'm not certain...but perhaps the external function definition can't access the private struct A in the implementation? Seems a bit odd though.
struct A is within the namespace of struct test

Change
A* test::getRowFromTable(int index)
to
test::A* test::getRowFromTable(int index)
Thanks a lot!!
It really helped since I wasted a lot of time for this petty issue!
But I used to previously use the same way without any errors!
It will be helpful if you can explain the details since I have declared the struct within the class definition. Then why do we need to explicitly specify test::A* !
Your method definition isn't in the class scope, so it doesn't know about the A struct declared there.
I am completely confused!
Just now it was working and again it started to produce errors!
The code is completely same as follows:

//test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef __TEST_H
#define __TEST_H

#include <vector>

class test {

  struct A {
    int a;
    int b;
  };
 

 public:

  vector<A*> table;
  test();
  A* getRowFromTable(int index) const;
};

 
#endif 


//test.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "test.h"

test::test() {
  for(int i=0; i<2; i++) {
    A* mem = new A;
    mem->a = i+10;
    mem->b = i*10;
    table.push_back(mem);
  }
}

test::A* test::getRowFromTable(int index) const {
  return table[index];
}


The error message is as follows:

In file included from test.C:1:
test.h:16: error: ISO C++ forbids declaration of ‘vector’ with no type
test.h:16: error: expected ‘;’ before ‘<’ token
test.C: In constructor ‘test::test()’:
test.C:8: error: ‘table’ was not declared in this scope
test.C: In member function ‘test::A* test::getRowFromTable(int) const’:
test.C:13: error: ‘table’ was not declared in this scope
Does using std::vector give the same errors?
in test.h make these changes.

#include <vector>
using namespace std;

it must work.
compile it as before.

regards
Topic archived. No new replies allowed.