initializing with return value of a function

R

Ralf Goertz

Hi,

why does the following not work:

#include <vector>

class foo
{
const size_t n;
const vector<int> &vec;
public:
foo(const std::vector<int> &v) : vec(&v),n(v.size()) {}
};


I get the following compilation error:

argument of type 'size_t (std::vector<int, std::allocator<int> >::
()const' does not match 'const size_t'

It seems as if I can't use the return value of a function as an
initializer. Why is that the case? And, more importantly, how can I
initialize foo::n with the size of v?

Ralf
 
M

Micah Cowan

Ralf Goertz said:
Hi,

why does the following not work:

#include <vector>

class foo
{
const size_t n;
const vector<int> &vec;
public:
foo(const std::vector<int> &v) : vec(&v),n(v.size()) {}
^

The underscored ampersand (&) is erroneous, and causes an attempt to
initialize vec with a pointer to v, rather than v itself. If you
_wanted_ a pointer, then vec has been misdeclared (as a reference,
rather than a pointer).
I get the following compilation error:

argument of type 'size_t (std::vector<int, std::allocator<int> >::
()const' does not match 'const size_t'

Are you sure? This looks very much like it was mangled; it makes
little sense (and is missing a parenthesis).
It seems as if I can't use the return value of a function as an
initializer. Why is that the case?

It isn't.
 
I

Ian Collins

Ralf said:
Hi,

why does the following not work:

#include <vector>

class foo
{
const size_t n;
const vector<int> &vec;
public:
foo(const std::vector<int> &v) : vec(&v),n(v.size()) {}
};


I get the following compilation error:

argument of type 'size_t (std::vector<int, std::allocator<int> >::
()const' does not match 'const size_t'
What ever code gave this error isn't the code you posted. I bet you
wrote something like n(v.size).
It seems as if I can't use the return value of a function as an
initializer. Why is that the case?
It isn't. The error simply doesn't match the code, which has numerous
other syntax errors.
 
T

Triple-DES

Hi,

why does the following not work:

#include <vector>

class foo
{
    const size_t n;
    const vector<int> &vec;

You probably intended to write:
const std::vector said:
public:
    foo(const std::vector<int> &v) : vec(&v),n(v.size()) {}

Should be:
foo(const std::vector said:
};

I get the following compilation error:

argument of type 'size_t (std::vector<int, std::allocator<int> >::
()const' does not match 'const size_t'
Not with the example you posted.

DP
 
R

Ralf Goertz

Ian said:
What ever code gave this error isn't the code you posted. I bet you
wrote something like n(v.size).

And you win. In the real code I have two vectors and two sizes and
forgot the () after the second size. Sorry for bothering.
It isn't. The error simply doesn't match the code, which has numerous
other syntax errors.

Yes it has. I thought I can write such a simple example without error.

Thanks and sorry,

Ralf
 

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