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

Vector and classes

P: n/a
Hi,

I am new to C++ and have run into the following problem. I have written
some functions which return a vector e.g

vector<doublemyfunction(double arg1,.......,.....)
{

}
I woud like to create a class out of this functions, however I dont
know how to write the header file correctly, when i use:

#ifndef MYCLASS_H
#define MYCLASS_H

vector<doublemyfunction(double arg1,.......,.....);

#endif

I get the error the vector is not template. Would be great if somebody
could help.

Happy x-mas to everyone

Michael

Dec 24 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Michael wrote:
Hi,

I am new to C++ and have run into the following problem. I have written
some functions which return a vector e.g

vector<doublemyfunction(double arg1,.......,.....)
{

}
I woud like to create a class out of this functions, however I dont
know how to write the header file correctly, when i use:

#ifndef MYCLASS_H
#define MYCLASS_H
#include <vector>
>
vector<doublemyfunction(double arg1,.......,.....);
std::vector<doublemyfunction(double arg1,.......,.....);
>
#endif

I get the error the vector is not template. Would be great if somebody
could help.
Firstly you need to include <vectorand second you need to specify
which namespace. Since you're using it in a header, you should not do a
"using namespace std". My preference is to fully quality the namespace
on each use in the header.
Dec 24 '06 #2

P: n/a

Michael wrote:
Hi,

I am new to C++ and have run into the following problem. I have written
some functions which return a vector e.g

vector<doublemyfunction(double arg1,.......,.....)
{

}
I woud like to create a class out of this functions, however I dont
know how to write the header file correctly, when i use:

#ifndef MYCLASS_H
#define MYCLASS_H

vector<doublemyfunction(double arg1,.......,.....);
std::vector<doublemyfunction(double arg1,.......,.....);

Also note that this is quite fragile performance-wise, if RVO is not
used to optimize out the copy of vector (-because of inferior
compiler or specific situation). Perhaps the better solution here is

myfunction(std::vector<double>& , ....);

Dec 24 '06 #3

P: n/a
Mirek Fidler wrote:
Michael wrote:
>Hi,

I am new to C++ and have run into the following problem. I have
written some functions which return a vector e.g

vector<doublemyfunction(double arg1,.......,.....)
{

}
I woud like to create a class out of this functions, however I dont
know how to write the header file correctly, when i use:

#ifndef MYCLASS_H
#define MYCLASS_H

vector<doublemyfunction(double arg1,.......,.....);

std::vector<doublemyfunction(double arg1,.......,.....);

Also note that this is quite fragile performance-wise, if RVO is
not used to optimize out the copy of vector (-because of inferior
compiler or specific situation). Perhaps the better solution here is

myfunction(std::vector<double>& , ....);
Could be, but perhaps we should save that until we see that there is a
performance problem? Michael didn't say how many millions of doubles the
vector is expected to contain.

Until we know that, returning a vector is perfectly good, especially if it
makes the code easier to read and understand.

Bo Persson
Dec 24 '06 #4

P: n/a
Gianni Mariani wrote:
Michael wrote:
>Hi,

I am new to C++ and have run into the following problem. I have written
some functions which return a vector e.g

vector<doublemyfunction(double arg1,.......,.....)
{

}
I woud like to create a class out of this functions, however I dont
know how to write the header file correctly, when i use:

#ifndef MYCLASS_H
#define MYCLASS_H

#include <vector>
>>
vector<doublemyfunction(double arg1,.......,.....);

std::vector<doublemyfunction(double arg1,.......,.....);
>>
#endif

I get the error the vector is not template. Would be great if somebody
could help.

Firstly you need to include <vectorand second you need to specify
which namespace. Since you're using it in a header, you should not do a
"using namespace std". My preference is to fully quality the namespace
on each use in the header.
Unfortunately, there are no typedef templates yet. Otherwise, I'd suggest to
typedef std::vector into an own namespace and then use that in all headers.

Dec 24 '06 #5

P: n/a
"Bo Persson" <bo*@gmb.dkwrote:
Mirek Fidler wrote:
std::vector<doublemyfunction(double arg1,.......,.....);

Also note that this is quite fragile performance-wise, if RVO is
not used to optimize out the copy of vector (-because of
inferior compiler or specific situation). Perhaps the better
solution here is

myfunction(std::vector<double>& , ....);

Could be, but perhaps we should save that until we see that there is
a performance problem? Michael didn't say how many millions of
doubles the vector is expected to contain.

Until we know that, returning a vector is perfectly good, especially
if it makes the code easier to read and understand.
I don't like premature optimization, but let's not prematurely pessimize
our program either. Because this particular issue requires an interface
change, it bears more scrutiny up front.

Mirek's suggestion not only is likely to perform better, it is also not
likely to perform any worse, and it is more idomatic, if a little C like.

Some other possible solutions:

const vector<double>& myfunction( /*params*/ ) {
static vector<doubleresult;
result.clear();
// fill result
return result;
}

The above has the benefit of being pratically a drop-in replacement for
the OPs function signature, but has the drawback of keeping a possible
extra copy of the result indefinitely.

Another solution which fits better with the rest of the library would be:

template < typename OutIt >
void myfunction( OutIt begin, /*params*/ );

This solution allows the caller to choose the container that best suits
his needs rather than having to use vector.

Another:

struct MyGenerator {
MyGenerator( /*params*/ );
double operator()(); // returns the next double in the list
};

The above can be used with either the generate or generate_n functions,
but would require the caller to know how many elements should exist.

Lastly, we can make an iterator:

class MySequence: public std::iterator< std::forward_iterator_tag,
double >
{
public:
MySequence(); // sentinel
explicit MySequence(/*params*/); // start the sequence

double operator*(); // return the current value in the sequence

MySequence& operator++(); // go to the next value in the sequence
MySequence operator++(int);

friend bool operator==(const MySequence& lhs, const MySequence& rhs);
// return true if the both objects point to the same element
// in the sequence.
};

bool operator!=(const primes& lhs, const primes& rhs) {
return !(lhs == rhs);
}

This can be used directly in other algorithms without having to ever
create a container holding all the elements. (I have, for example,
something like the above for generating primes and fibonacci numbers.)
But it may be too heavy weight for the OPs situation.
Dec 24 '06 #6

P: n/a

Daniel T. wrote:
Some other possible solutions:

const vector<double>& myfunction( /*params*/ ) {
static vector<doubleresult;
result.clear();
// fill result
return result;
}
Beware of this soltution. Think e.g. about

add(myfunction(...), myfunction(...));

Dec 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.