/*
vector of Person
*/
#include <iostream>
#include <vector>
usingnamespace std;
int main(int argc, char** argv) {
struct Person
{
char * name;
char * surname;
char * street;
int number;
};
vector <Person> lista (10);//Why can't I initialize lista with 10??
return 0;
}
19 17 C:\Users\Zwilu\Desktop\vector\main.cpp [Error] template argument for 'template<class> class std::allocator' uses local type 'main(int, char**)::Person'
19 17 C:\Users\Zwilu\Desktop\vector\main.cpp [Error] trying to instantiate 'template<class> class std::allocator'
19 17 C:\Users\Zwilu\Desktop\vector\main.cpp [Error] template argument 2 is invalid
19 25 C:\Users\Zwilu\Desktop\vector\main.cpp [Error] invalid type in declaration before '(' token
Because struct goes outside int main() and to get name of a person or things like that other than a single character, you use string not char * except you are learning C c++.
It's because Person is local to the function. I don't really understand it, but I could see that there would be a problem if the Person type is expected to go out of scope at the end of the function and the templated data within the vector is expected to persist.
and to get name of a person or things like that other than a single character, you use string not char * except you are learning C c++.
not true... there are plenty of valid reasons to use a char * over std::string.
Strings exist in C++ and that's what they are for!
char *'s are strings as well.
@op: why dont you want it global? i cant remember how, but i think you can make the struct static which would resolve the issue. you could also make it global and wrap it in a class namespace. you could use a class but a) it wouldnt make sense if you dont want it global and b) it uses memory unneccesarily
Strings exist in C++ and that's what they are for!
I know that strings exist, but, as you probably know, many librarys are written with the C strings style, and many functions use pointers! I want to learn pointers, 'cause they are harder to understand and use than string class (type) of the C++!
why dont you want it global?
As we can declare an int or a string or also a struct inside a function or the main function, in this case, I would also to know why can't I do the same!!!
Really? I'd like to know some of them, perhaps you could even teach Bjarne Stroustrup!
if i wanted to create an array of valid file extensions, char char *extensions[] = {".whatev" ... } would take up less space then creating multiple instances of std::string
The amount of misinformation in this thread is scary
I don't want to carray ten ton weights I just want to have the power to carry them if sometimes its necessary! omg, people are so [squared] :(
99% of the time it is not necessary. Only optimize if your profiler shows that it is a bottleneck.
xkcd reference wrote:
if i wanted to create an array of valid file extensions, char char *extensions[] = {".whatev" ... } would take up less space then creating multiple instances of std::string
Hardly. It's far more dangerous, harder to manage, and way too early of an optimization. Use a std::vector<std::string>. memory usage should be the least of your concerns unless you profile you code and find that this is the only way to reduce memory usage, even even so this will hardly help.
99% of the time it is not necessary. Only optimize if your profiler shows that it is a bottleneck.
I don't think so! "Sometimes" you have to understand or you want to understand how librarys are written and if you are a "0 on the left" with pointers you probably should pray!
As we can declare an int or a string or also a struct inside a function or the main function, in this case, I would also to know why can't I do the same!!!
You can declare objects of these types in a function, not a definition of the type itself. (may have changed for C++11?)
Move the struct declaration/definition out of main(). Stick it in its own namespace if it would make you feel better. Then, declare an object of type YourNamespace::Person in main().
EDIT: This may only be a restriction when attempting to use the type in a template. I need to read more.