Vector in cpp

M

Marcelo

Hello,

I would like to know how to use the std::vector variable.
I have tried a class something like this, but I get an error and I don't know why...
the error
lib/image.h:13: error: using-declaration for non-member at class scope
lib/image.h:13: error: expected ‘;’ before ‘<’ token

the code
#ifndef IMAGE_H
#define IMAGE_H

#include <stdio.h>

/*Create the class for the images */
class Image {
public:
int w; // The width of the image.
int h; // The height of the image.
int dim; // The dimension of the the image.
//The array RGB type uchar of the image.
std::vector<unsigned char> arr;
};

#endif /* IMAGE_H */
 
R

roberts.noah

Marcelo said:
Hello,

I would like to know how to use the std::vector variable.
I have tried a class something like this, but I get an error and I don't know why...
the error
lib/image.h:13: error: using-declaration for non-member at class scope
lib/image.h:13: error: expected ';' before '<' token

the code
#ifndef IMAGE_H
#define IMAGE_H

#include <stdio.h>
 
M

Marcelo

I suppose that i need this, because then the compilation works just fine.

#include <vector.h>

However, where can I have more documentation about vector type?

thanks a lot

MArcelo
 
W

W Marsh

Hello,

I would like to know how to use the std::vector variable.
I have tried a class something like this, but I get an error and I don't know why...
the error
lib/image.h:13: error: using-declaration for non-member at class scope
lib/image.h:13: error: expected ‘;’ before ‘<’ token

the code
#ifndef IMAGE_H
#define IMAGE_H

#include <stdio.h>

/*Create the class for the images */
class Image {
public:
int w; // The width of the image.
int h; // The height of the image.
int dim; // The dimension of the the image.
//The array RGB type uchar of the image.
std::vector<unsigned char> arr;
};

#endif /* IMAGE_H */

You forgot #include <vector>.

Also, stdio.h isn't very C++. Are you still using printf? Consider the
alternatives C++ offers you.
 
R

roberts.noah

Marcelo said:
I suppose that i need this, because then the compilation works just fine.

#include <vector.h>

You are using old, prestandard headers. Leave out the .h

Get "The C++ Standard Library" by Josuttis for documentation.
 
O

osmium

Marcelo said:
However, where can I have more documentation about vector type?

The first two Google hits on <stl reference> are the two main on-line
references, Dinkumware and SGI (nee Sun). Being reference there are pretty
cryptic but there is a lot more on Google you can probably find as easily
for yourself.
 
M

Mike Smith

W said:
Also, stdio.h isn't very C++. Are you still using printf? Consider the
alternatives C++ offers you.

C++ itself doesn't really offer a good alternative to printf(), IMO.
Boost does, however.
 
D

Default User

Mike said:
C++ itself doesn't really offer a good alternative to printf(), IMO.

I agree, I still use printf when I need a compact way to do formatted
output. I'm not sure what the objection is.
Boost does, however.

Bah, third party, off-topic library :)



Brian
 
M

Mike Smith

Default said:
Mike Smith wrote:




I agree, I still use printf when I need a compact way to do formatted
output. I'm not sure what the objection is.

Well, apart from being seen as gauche by the "new C++" crowd, it is kind
of a pain. Suppose you want to generate a string formatted
printf()-style, to do it using otherwise C++-ish style you need to jump
through hoops:

int a = 5;
float b = 3.14;
string c = "blah";

char tmpbuf[HOW_MUCH_TO_ALLOCATE_HERE];
sprintf(tmpbuf, "a = %08d, b = %.2f, c = %s", a, b, c.c_str());
string s(tmpbuf);

It's ironic to have to use character arrays for this sort of thing, when
one of the very reasons that C++ strings were introduced in the first
place was... to eliminate the need to work with character arrays. :-/
 
B

baalbek

Come on, dude!

std::eek:stringsream is way better than any printf!

You just need to include the right headers!

What's better:

printf("%s %d", "test", 12);

or this:

std::eek:stringstream o;
cout << o << "test" << 12;

Baalbek
 
N

Neil Cerutti

Come on, dude!

std::eek:stringsream is way better than any printf!

You just need to include the right headers!

What's better:

printf("%s %d", "test", 12);

or this:

std::eek:stringstream o;
cout << o << "test" << 12;

Now write iostream code to do this:

printf("%10.10f %f %.0f" 1000.48702, 23.718, 800.0);
 
M

Marcelo Pinto

baalbek escreveu:
Come on, dude!

std::eek:stringsream is way better than any printf!

You just need to include the right headers!

What's better:

printf("%s %d", "test", 12);

or this:

std::eek:stringstream o;
cout << o << "test" << 12;
You forgot the white space after *test*.

The thing I dislike in printf (and family) is:

printf("%d %s", "test ", 12);

The invertion is not clear and the results may be surprising.

HTH,

Marcelo Pinto
 
N

Neil Cerutti

baalbek escreveu:

You forgot the white space after *test*.

The thing I dislike in printf (and family) is:

printf("%d %s", "test ", 12);

The invertion is not clear and the results may be surprising.

Yup. fprintf and friends provide an excellent and convenient
notation, but they are not type safe or extensible. iostreams
provide for type-safe and extensible formatting, at the expense
of convenient notation.

On the other hand, extensibility means that a convenient notation
is possible to implement yourself, as boost did.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top