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

strange template class error

P: n/a

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>::operator=(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_[i];
}

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

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

sv(2) = 1.;

return 0;
}

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

Can anyone figure out what's happening ?

Thanks.
Oct 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Valeriu Catina wrote:
g++ gives the next error messages

[...]
int main(void)
{
SmallVector<double,4sv(SmallVectorNoInit());
That's a declaration of a function. To overcome this C++ "problem",
surround the argument with an extra set of parentheses:

SmallVector<double,4sv((SmallVectorNoInit()));
>
sv(2) = 1.;

return 0;
}

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

Can anyone figure out what's happening ?
Learn to read the error messages.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #2

P: n/a
Valeriu Catina wrote:
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>::operator=(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_[i];
}

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

};
int main(void)
{
SmallVector<double,4sv(SmallVectorNoInit());
Add a set of parentheses here:

SmallVector<double,4sv(( SmallVectorNoInit() ));
>
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

Oct 3 '06 #3

P: n/a
Valeriu Catina wrote:
SmallVector<double,4sv(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.
Oct 3 '06 #4

P: n/a
Victor Bazarov posted:

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

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

SmallVector<double,4sv((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,4sv(SmallVectorNoInit);

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

--

Frederick Gotham
Oct 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.