473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Vector in cpp

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
12 6833

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
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
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

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
"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
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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
16
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9:...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.