Create a vector of typedef struct, how to?
Nov 1, 2009 at 6:14am Nov 1, 2009 at 6:14am UTC
Hi,
I want to create a std::vector of typedef struct. Any std::vector position will store a "vetor" that is a typedef struc...
Actually I did this:
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
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <iomanip>
#include <dirent.h>
#include <errno.h>
using namespace std;
/*
*
*/
int main(int argc, char ** argv) {
typedef struct mywp{
int line, col, index;
} waypoint;
typedef struct v1{
waypoint wp;
} vetor;
std::vector< vetor > waypoints;
return (EXIT_SUCCESS);
}
I am getting the error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/cristiano/NetBeansProjects/waypoints'
/usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/waypoints
make[2]: Entering directory `/home/cristiano/NetBeansProjects/waypoints'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
main.cpp: In function 'int main(int, char**)' :
main.cpp:39: error: template argument for 'template<class _Alloc> class std::allocator' uses local type 'main(int, char**)::v1'
main.cpp:39: error: trying to instantiate 'template<class _Alloc> class std::allocator'
main.cpp:39: error: template argument 2 is invalid
main.cpp:39: error: invalid type in declaration before ';' token
make[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
make[2]: Leaving directory `/home/cristiano/NetBeansProjects/waypoints'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/cristiano/NetBeansProjects/waypoints'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 581ms)
Any idea of what I am doing wrong?
Nov 1, 2009 at 6:51am Nov 1, 2009 at 6:51am UTC
The error message is pretty clear. You're trying to make an instance of std::vector<T>, which you can't.
Oh, and typedef structs are a C thing. C++ has a more convenient syntax:
1 2
struct waypoint{ int line,col,index; };
struct vetor{ waypoint wp; };
Nov 1, 2009 at 2:22pm Nov 1, 2009 at 2:22pm UTC
But I need a std::vector which type is the new type I am creating..
There is no way I can do such thing?
I changed my code to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main(int argc, char ** argv) {
struct waypoint {
int line, col, index;
};
struct vetor {
waypoint wp;
};
std::vector< vetor > waypoints;
return (EXIT_SUCCESS);
}
And I am getting the error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/cristiano/NetBeansProjects/waypoints'
/usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/waypoints
make[2]: Entering directory `/home/cristiano/NetBeansProjects/waypoints'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
main.cpp: In function 'int main(int, char**)' :
main.cpp:39: error: template argument for 'template<class _Alloc> class std::allocator' uses local type 'main(int, char**)::vetor'
main.cpp:39: error: trying to instantiate 'template<class _Alloc> class std::allocator'
main.cpp:39: error: template argument 2 is invalid
main.cpp:39: error: invalid type in declaration before ';' token
make[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
make[2]: Leaving directory `/home/cristiano/NetBeansProjects/waypoints'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/cristiano/NetBeansProjects/waypoints'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 809ms)
Nov 1, 2009 at 2:32pm Nov 1, 2009 at 2:32pm UTC
You can't use local types in a standard container.
Also, your
vetor type doesn't have much point. Why use it?
1 2 3 4 5 6 7 8 9 10 11 12
struct waypoint {
int line, col, index;
};
int main(int argc, char ** argv) {
std::vector<waypoint> waypoints;
...
return 0;
}
Personally, I
like the EXIT_SUCCESS and EXIT_FAILURE macros, but they are only #defined in <cstdlib>, which I don't think you ought to have in your list of #includes... (Or if you do actually require it, use <cstdlib> instead of <stdlib.h>.)
Hope this helps.
Nov 1, 2009 at 2:50pm Nov 1, 2009 at 2:50pm UTC
Thanks guys,
problem solved!
Topic archived. No new replies allowed.