By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,132 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,132 IT Pros & Developers. It's quick & easy.

Vector in cpp

P: n/a
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 */
Dec 14 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

Marcelo wrote:
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> #include <vector> // I would also change above to cstdio or use c++
streams.
/*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 */


Dec 14 '05 #2

P: n/a
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
Dec 14 '05 #3

P: n/a
On Wed, 14 Dec 2005 18:25:40 +0100, Marcelo <ma********@yahoo.com>
wrote:
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.
Dec 14 '05 #4

P: n/a

Marcelo wrote:
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.

Dec 14 '05 #5

P: n/a
"Marcelo" writes:
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.
Dec 14 '05 #6

P: n/a
W Marsh wrote:

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.

--
Mike Smith
Dec 14 '05 #7

P: n/a
Mike Smith wrote:
W Marsh wrote:

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.


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

Dec 14 '05 #8

P: n/a
Default User wrote:
Mike Smith wrote:

W Marsh wrote:
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.

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. :-/

--
Mike Smith
Dec 15 '05 #9

P: n/a
Come on, dude!

std::ostringsream 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::ostringstream o;
cout << o << "test" << 12;

Baalbek
Mike Smith wrote:
W Marsh wrote:

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.

--
Mike Smith

Dec 16 '05 #10

P: n/a
On 2005-12-16, baalbek <rc*@bgoark.no> wrote:
Come on, dude!

std::ostringsream 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::ostringstream o;
cout << o << "test" << 12;


Now write iostream code to do this:

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

--
Neil Cerutti
Dec 16 '05 #11

P: n/a

baalbek escreveu:
Come on, dude!

std::ostringsream 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::ostringstream 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

Dec 16 '05 #12

P: n/a
On 2005-12-16, Marcelo Pinto <mp******@gmail.com> wrote:

baalbek escreveu:
Come on, dude!

std::ostringsream 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::ostringstream 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.


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.

--
Neil Cerutti
Dec 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.