vectors

Pages: 12
closed account (jvqpDjzh)
Why can't I do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
vector of Person
*/
#include <iostream>
#include <vector>
using namespace 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;
}
Last edited on
What you do mean you can't?
closed account (jvqpDjzh)

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
closed account (iAk3T05o)
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++.
Last edited on
with the keyword typedef, the compiler treats as a kind person and not as a structure ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
using namespace std;

typedef struct
{
	char * name;	
	char * surname;
	char * street;
	int number; 
}Person;

int main(int argc, char** argv) {
		
	vector <Person> lista (10);

	return 0;
}
Last edited on
closed account (jvqpDjzh)
you use string not char *[/code] except you are learning C c++.

Pointers exist also in C++!


Because struct goes outside int main()

Why should the struct go outside main()?
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.

Example, what if you did this?
1
2
3
4
5
6
void someFunc()
{
  struct Something {};

  vector<Something> *pLista = new vector<Something>();
}


The data would persist, but the instructions associated with dereferencing it would be lost (though I suppose it's the same as using void pointers).

If I make Person a permanent type ( global definition or class definition ) it works ok.
Last edited on
closed account (jvqpDjzh)
the compiler treats as a kind person and not as a structure ..

?

I don't want the struct outside main! Person should be already a type (struct) that i will use to define my members in the vector lista!
closed account (iAk3T05o)
Strings exist in C++ and that's what they are for!
closed account (jvqpDjzh)
but if we declare the struct as static inside main shouldn't it work?NO! why doesn't it work?
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
Last edited on
xkcd reference wrote:
not true... there are plenty of valid reasons to use a char * over std::string.
Really? I'd like to know some of them, perhaps you could even teach Bjarne Stroustrup!

The amount of misinformation in this thread is scary.
Last edited on
closed account (jvqpDjzh)

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!!!
Last edited on
zwiluwu wrote:
I want to learn pointers, 'cause they are harder to understand and use than string class (type) of the C++!
And I want to carry ten ton weights on my back everywhere because they are harder to carry than backbacks.
closed account (jvqpDjzh)
And I want to carry ten ton weights on my back everywhere because they are harder to carry than backbacks


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] :(
Last edited on
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 agree
zwiluqu wrote:
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.
closed account (jvqpDjzh)
L B

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!
Last edited on
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.
Last edited on
LB wrote:
99% of the time [you don't have to]
zwiluwu wrote:
"Sometimes" you have to
What do you think "sometimes" means?

It is fine to understand how pointers and C-style strings work. It is not fine to use them excessively.
Last edited on
Pages: 12