strange template class error

V

Valeriu Catina

Hi,

g++ gives the next error messages

test.cpp: In function 'int main()':
test.cpp:53: error: invalid conversion from 'int' to 'SmallVectorNoInit
(*)()'
test.cpp:53: error: initializing argument 1 of 'SmallVector<double, 4>
sv(SmallVectorNoInit (*)())'
test.cpp:53: error: no match for 'operator=' in 'sv(SmallVectorNoInit
(*)())() = 1.0e+0'
test.cpp:6: note: candidates are: SmallVector<double, 4>&
SmallVector<double, 4>::eek:perator=(const SmallVector<double, 4>&)

when it compiles the code below:


////////////////////////////////////////////////


struct SmallVectorNoInit {};


template<class P_numtype, int N_length>
class SmallVector
{

private:
P_numtype data_[N_length];


public:

// --- length
int length() const
{ return N_length; }


// --- constructors
SmallVector()
{
//meta_Assign<N_length, 0>
//::Assign(data_, P_numtype(), _bz_update<P_numtype, P_numtype>());
}

SmallVector(const SmallVector& from)
{
//meta_Assign<N_length, 0>
// ::Assign(data_, from.data_, _bz_update<T_numtype, T_numtype>());
}

explicit SmallVector(const SmallVectorNoInit&)
{ }


P_numtype& operator ()(int i)
{
return data_;
}

const P_numtype& operator ()(int i) const
{
return data_;
}

};


int main(void)
{
SmallVector<double,4> sv(SmallVectorNoInit());

sv(2) = 1.;

return 0;
}

////////////////////////////////////////////////

Can anyone figure out what's happening ?

Thanks.
 
V

Victor Bazarov

Valeriu said:
g++ gives the next error messages

[...]
int main(void)
{
SmallVector<double,4> sv(SmallVectorNoInit());

That's a declaration of a function. To overcome this C++ "problem",
surround the argument with an extra set of parentheses:

SmallVector said:
sv(2) = 1.;

return 0;
}

////////////////////////////////////////////////

Can anyone figure out what's happening ?

Learn to read the error messages.

V
 
M

mlimber

Valeriu said:
Hi,

g++ gives the next error messages

test.cpp: In function 'int main()':
test.cpp:53: error: invalid conversion from 'int' to 'SmallVectorNoInit
(*)()'
test.cpp:53: error: initializing argument 1 of 'SmallVector<double, 4>
sv(SmallVectorNoInit (*)())'
test.cpp:53: error: no match for 'operator=' in 'sv(SmallVectorNoInit
(*)())() = 1.0e+0'
test.cpp:6: note: candidates are: SmallVector<double, 4>&
SmallVector<double, 4>::eek:perator=(const SmallVector<double, 4>&)

when it compiles the code below:


////////////////////////////////////////////////


struct SmallVectorNoInit {};


template<class P_numtype, int N_length>
class SmallVector
{

private:
P_numtype data_[N_length];


public:

// --- length
int length() const
{ return N_length; }


// --- constructors
SmallVector()
{
//meta_Assign<N_length, 0>
//::Assign(data_, P_numtype(), _bz_update<P_numtype, P_numtype>());
}

SmallVector(const SmallVector& from)
{
//meta_Assign<N_length, 0>
// ::Assign(data_, from.data_, _bz_update<T_numtype, T_numtype>());
}

explicit SmallVector(const SmallVectorNoInit&)
{ }


P_numtype& operator ()(int i)
{
return data_;
}

const P_numtype& operator ()(int i) const
{
return data_;
}

};


int main(void)
{
SmallVector<double,4> sv(SmallVectorNoInit());


Add a set of parentheses here:

SmallVector said:
sv(2) = 1.;

return 0;
}

////////////////////////////////////////////////

Can anyone figure out what's happening ?

The problem is that it thinks you are declaring a function named sv
(the rule is that anything that can be a function definition is
interpreted that way, and here the parentheses by which you intended to
create a temporary instance of SmallVectorNoInit are simply ignored.)

Cheers! --M
 
R

Ron Natalie

Valeriu said:
SmallVector<double,4> sv(SmallVectorNoInit());

This declares a function called sv that takes an argument
of SmallVectorNoInit NOT an object initialized with one.
sv(2) = 1.;

This attempts to call the function passing a int to the
parameter which is type SmallVectorNoInit.
 
F

Frederick Gotham

Victor Bazarov posted:

That's a declaration of a function. To overcome this C++ "problem",
surround the argument with an extra set of parentheses:

SmallVector<double,4> sv((SmallVectorNoInit()));


This nuisance came about because C++ was built on top of C, and so there
came ambiguity when identical syntax was chosen for object initialisation
as is used for function declarations (which also contributed to the need
for "typename" for dependant base types). It can be quite annoying until
you engrave the following paragraph on the inside of your skull:

Stare at it... if you fathom in any way that it
could possibly be a function declaration, then
it's a function declaration.

You'd be surprised how nonchalantly the compiler can peel away parentheses
-- it interpreted your original line as:

SmallVector<double,4> sv(SmallVectorNoInit);

As you can see, this is a declaration for a function which takes a
SmallVectorNoInit by value.
 

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

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top