Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > non-constant reference

Reply
Thread Tools

non-constant reference

 
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      07-19-2005
Consider the source snippet.

class foo_base
{};

template <typename T>
class foo : public foo_base
{
public:
typedef void (T::*F)();
foo( T& t, F f) : t_(&t), f_(f) {}
void operator()() const
{
(t_->*f_)();
}
private:
T* t_;
F f_;
};

template <class T>
std::auto_ptr<foo_base> new_cb(T& t, void (T::*f)())
{
return std::auto_ptr<foo_base>(new foo<T>(t, f));
}

---------
class my_class {
static std::auto_ptr<foo_base> test;
public:
template <typename T>
my_class ( T& t, void (T::*f)()) {
test = new_cb(t, f); /// 2
}
};
---------

The line indicated by the number '2' works on the .NET 03 compiler but
gcc complains about initilization of non-constant reference.

C:\SW\MJT\Test\test.h:200: initialization of non-const reference
type `class auto_ptr<foo_base> &'
C:\SW\MJT\Test\test.h:200: from rvalue of type `auto_ptr<foo_base>'

'long winded directory deleted'
include\g++-3\memory:40: in passing argument 1 of
`auto_ptr<foo_base>:perator =(auto_ptr<foo_base> &)'


Not sure if i follow the error and what the solution is. Thanks in
advance.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Consider the source snippet.
>
> class foo_base
> {};
>
> template <typename T>
> class foo : public foo_base
> {
> public:
> typedef void (T::*F)();
> foo( T& t, F f) : t_(&t), f_(f) {}
> void operator()() const
> {
> (t_->*f_)();
> }
> private:
> T* t_;
> F f_;
> };
>
> template <class T>
> std::auto_ptr<foo_base> new_cb(T& t, void (T::*f)())
> {
> return std::auto_ptr<foo_base>(new foo<T>(t, f));
> }
>
> ---------
> class my_class {
> static std::auto_ptr<foo_base> test;
> public:
> template <typename T>
> my_class ( T& t, void (T::*f)()) {
> test = new_cb(t, f); /// 2
> }
> };
> ---------
>
> The line indicated by the number '2' works on the .NET 03 compiler but
> gcc complains about initilization of non-constant reference.


Not enough information. How do you use 'my_class'? If I add

std::auto_ptr<foo_base> my_class::test;

struct fubar {
void bar() {}
};

int main() {
fubar f;
my_class m(f, &fubar::bar);
}

to the end of your code, it compiles fine with Comeau online (provided
I also include <memory> at the beginning).

> C:\SW\MJT\Test\test.h:200: initialization of non-const reference
> type `class auto_ptr<foo_base> &'
> C:\SW\MJT\Test\test.h:200: from rvalue of type `auto_ptr<foo_base>'
>
> 'long winded directory deleted'
> include\g++-3\memory:40: in passing argument 1 of
> `auto_ptr<foo_base>:perator =(auto_ptr<foo_base> &)'
>
>
> Not sure if i follow the error and what the solution is. Thanks in
> advance.


Provide _complete_ code.

V
 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      07-19-2005
Victor Bazarov wrote:

> Not enough information. How do you use 'my_class'? If I add
>
> std::auto_ptr<foo_base> my_class::test;
>
> struct fubar {
> void bar() {}
> };
>
> int main() {
> fubar f;
> my_class m(f, &fubar::bar);
> }
>
> to the end of your code, it compiles fine with Comeau online (provided
> I also include <memory> at the beginning).
>


Compiles for me with GCC 3.3.5 20050117 (prerelease) (SUSE Linux)

<shrug>
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      07-19-2005
Steven T. Hatton wrote:
> Victor Bazarov wrote:


>
> Compiles for me with GCC 3.3.5 20050117 (prerelease) (SUSE Linux)


But the template is not invoked (so you don't see the problem).
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2005
Gianni Mariani wrote:
> Steven T. Hatton wrote:
>
>> Victor Bazarov wrote:

>
>
>>
>> Compiles for me with GCC 3.3.5 20050117 (prerelease) (SUSE Linux)

>
>
> But the template is not invoked (so you don't see the problem).


What do you know that we don't?
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      07-19-2005
Victor Bazarov wrote:
> Gianni Mariani wrote:
>
>> Steven T. Hatton wrote:
>>
>>> Victor Bazarov wrote:

>>
>>
>>
>>>
>>> Compiles for me with GCC 3.3.5 20050117 (prerelease) (SUSE Linux)

>>
>>
>>
>> But the template is not invoked (so you don't see the problem).

>
>
> What do you know that we don't?


The line marked "/// 2" is within a template function that is never
called. Hence the problem that the poster alludes to will not be
identified by the compiler because the template is never instantiated.

Hence, your response is correct, the code is incomplete.

Apart from that, there are probably many things that I know that
everyone else does not know.

G
 
Reply With Quote
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      07-19-2005
Greetings Victor,

| Provide _complete_ code.

Makes no sense.. Will do

 
Reply With Quote
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      07-19-2005
Greetings Victor,

| Provide _complete_ code.

I'm obviously doing something wrong.. Will do

 
Reply With Quote
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      07-19-2005
-------------- sndr_exec.h --------------
#ifndef SNDR_EXEC_H
#define SNDR_EXEC_H

# include "sl_comm.h"
# include "com_header.h"

class sndr_exec
{
private:
Base *sndr_dy4;
slDev* pslDevice_;
public:
sndr_exec();
~sndr_exec();
void test_func();
void my_user_callback(slDev *pslDevice_,
unsigned int door_bell);
};

#endif // #ifndef sndr_exec

-------------- sndr_exec.cpp --------------

# include "sndr_exec.h"
std::auto_ptr<test_base> Base::user_cb_func;
sndr_exec::sndr_exec()
: sndr_dy4(0)
, pslDevice_(0)
{
sndr_dy4 = new(std::nothrow) Derived (
*this,
&sndr_exec::my_user_callback
);
if (!sndr_dy4)
return;
}

sndr_exec::~sndr_exec() {}

void sndr_exec::my_user_callback(
slDev *pslDevice_,
unsigned int door_bell)
{
std::cout << " this works YEAH " << std::endl;
std::cout << " door_bell " << door_bell << std::endl;
}
void sndr_exec::test_func()
{
sndr_dy4->sl_callback_func(pslDevice_, 99);
}


-------------- sl_comm.h --------------
#ifndef SL_COMM_H
#define SL_COMM_H
# include "com_header.h"

struct slDev
{
unsigned int slFabricId;
unsigned int slLinkId;
unsigned int slBridgeId;
void *arubaDev;
void *instance;
unsigned int pciIntAVector;
};

class test_base
{
public:
virtual void operator()(slDev *pslDevice_,
unsigned int bell_val) const {};
virtual ~test_base() = 0;
};

inline test_base::~test_base() {}

template <typename T>
class cb : public test_base
{
public:
typedef void (T::*F)(slDev *pslDevice_,
unsigned int bell_val);
cb( T& t, F f) : t_(&t), f_(f) {}
void operator()(slDev *pslDevice_, unsigned int bell_val) const
{
(t_->*f_)(pslDevice_, bell_val);
}
private:
T* t_;
F f_;
};

template <typename T>
cb<T> make_callback (
T& t,
void (T::*f)(slDev *pslDevice_,
unsigned int bell_val)
)
{
return cb<T>(t, f);
}

template <class T>
std::auto_ptr<test_base> new_cb(
T& t,
void (T::*f)(slDev *pslDevice_,
unsigned int bell_val)
)
{
return std::auto_ptr<test_base>(new cb<T>(t, f));
}

class Base {
private:
static std::auto_ptr<test_base> user_cb_func;
protected:

template <typename T>
Base(
T& t,
void (T::*f)(slDev *pslDevice_,
unsigned int bell_val))
{
user_cb_func = new_cb(t, f); // COMPLAINS
ABOUT THIS >>>>>
}

public:
static void sl_callback_func(
slDev *pslDevice_,
unsigned int bell_val
)
{
test_base& cb = *user_cb_func;
cb(&pslDevice_, bell_val);
}
virtual ~Base()
{}
};

class Derived : public Base {
private:
public:
template <typename T>
Derived(
T& t,
void (T::*f)(slDev *pslDevice_,
unsigned int bell_val) )
: Base(t, f)
{}

~Derived()
{}
};

#endif

-------------- com_header.h --------------
#ifndef COMM_HEADER
#define COMM_HEADER

// Standard C++
# include <cstdio>
# include <iostream>
# include <iomanip>
# include <stdlib.h>
# include <string>
# include <sstream>
# include <stdexcept>
# include <string>
# include <memory>
# include <functional>
# include <algorithm>
# include <iomanip>
# include <numeric>
# include <vector>
# include <list>
# include <map>
# include <ctime>
# include <bitset>
# include <typeinfo>

#endif // com_header.h

-------------- main_test.cpp --------------
#include "sndr_exec.h"

sndr_exec* sndr_exec_(0);

int main()
{
sndr_exec_ = new (std::nothrow) sndr_exec();
sndr_exec_->test_func();
delete sndr_exec;
}


The end result - gcc 2.96 complains about the line marked 'complains ..
above'.

> C:\SW\MJT\Test\sl_comm.h:200: initialization of non-const reference
> type `class auto_ptr<test_base> &'
> C:\SW\MJT\Test\sl_comm.h:200: from rvalue of type `auto_ptr<test_base>'


> 'long winded directory deleted'
> include\g++-3\memory:40: in passing argument 1 of
> `auto_ptr<test_base>:perator =(auto_ptr<test_base> &)'


Thanks again...

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2005
(E-Mail Removed) wrote:
> [...]


I simply put it all in one file and fixed a couple of compile errors. I
really don't like doing that, but here you go, this compiles. No, I did
not try to run it.

I am not trying to judge it (and don't ask me to), but next time don't
post things that have to be put in separate files to be compiled. If it's
a language problem you have, spend some effort reducing your code to the
point where it's all in one module and exhibits the same error.
--------------------------------------------------
# include <cstdio>
# include <iostream>
# include <iomanip>
# include <stdlib.h>
# include <string>
# include <sstream>
# include <stdexcept>
# include <string>
# include <memory>
# include <functional>
# include <algorithm>
# include <iomanip>
# include <numeric>
# include <vector>
# include <list>
# include <map>
# include <ctime>
# include <bitset>
# include <typeinfo>

struct slDev
{
unsigned int slFabricId;
unsigned int slLinkId;
unsigned int slBridgeId;
void *arubaDev;
void *instance;
unsigned int pciIntAVector;
};

class test_base
{
public:
virtual void operator()(slDev *pslDevice_, unsigned int bell_val)
const {}
virtual ~test_base() = 0;
};

inline test_base::~test_base() {}

template<typename T> class cb : public test_base
{
public:
typedef void (T::*F)(slDev *pslDevice_, unsigned int bell_val);

cb(T& t, F f) : t_(&t), f_(f) {}

void operator()(slDev *pslDevice_, unsigned int bell_val) const
{
(t_->*f_)(pslDevice_, bell_val);
}

private:
T* t_;
F f_;
};

template<typename T> cb<T> make_callback(T& t, void (T::*f)(slDev
*pslDevice_, unsigned int bell_val))
{
return cb<T>(t, f);
}

template<class T>
std::auto_ptr<test_base> new_cb(T& t, void (T::*f)(slDev *pslDevice_,
unsigned int bell_val))
{
return std::auto_ptr<test_base>(new cb<T>(t, f));
}

class Base {
private:
static std::auto_ptr<test_base> user_cb_func;
protected:

template<typename T> Base( T& t, void (T::*f)(slDev *pslDevice_,
unsigned int bell_val))
{
user_cb_func = new_cb(t, f); // COMPLAINS ABOUT THIS >>>>>
}

public:
static void sl_callback_func(slDev *pslDevice_, unsigned int bell_val)
{
test_base& cb = *user_cb_func;
cb(pslDevice_, bell_val);
}

virtual ~Base() {}
};

class Derived : public Base {
private:
public:
template<typename T> Derived(T& t, void (T::*f)(slDev *pslDevice_,
unsigned int bell_val))
: Base(t, f)
{}
};


class sndr_exec
{
private:
Base *sndr_dy4;
slDev* pslDevice_;
public:
sndr_exec();
~sndr_exec();
void test_func();
void my_user_callback(slDev *pslDevice_, unsigned int door_bell);
};

std::auto_ptr<test_base> Base::user_cb_func;

sndr_exec::sndr_exec()
: sndr_dy4(new (std::nothrow) Derived (*this, &sndr_exec::my_user_callback))
, pslDevice_(0)
{
}

sndr_exec::~sndr_exec() {}

void sndr_exec::my_user_callback(slDev *pslDevice_, unsigned int door_bell)
{
std::cout << " this works YEAH " << std::endl;
std::cout << " door_bell " << door_bell << std::endl;
}

void sndr_exec::test_func()
{
sndr_dy4->sl_callback_func(pslDevice_, 99);
}

sndr_exec *sndr_exec_ = 0;

int main()
{
sndr_exec_ = new (std::nothrow) sndr_exec();
sndr_exec_->test_func();
delete sndr_exec_;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET 2.0: master pages and web user controls: reference to a non-shared member requires an object reference bminder ASP .Net 0 06-24-2005 12:22 AM
asp.net 2005 question re: "reference to a non-shared member requires an object reference" ce ASP .Net 1 06-23-2005 09:15 PM
web reference interfering with reference to component Dude ASP .Net 0 11-09-2004 11:53 AM
How to tell if a reference is project or file reference from within the IDE? Darren ASP .Net 0 10-11-2004 12:51 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments