I'm newish to C++ but not to C.
I'm confused by this code. test1() is fine. test2() fails to
compile.
/tmp/inherit_ptr.cpp : In function âvoid test2()â:
/tmp/inherit_ptr.cpp :52: error: no matching function for call to
âboost::shared_ ptr<Base>::shar ed_ptr(test2(): :Derived2*)â
/usr/include/boost/shared_ptr.hpp: 119: note: candidates are:
boost::shared_p tr<T>::shared_p tr() [with T = Base]
/usr/include/boost/shared_ptr.hpp: 106: note:
boost::shared_p tr<Base>::share d_ptr(const boost::shared_p tr<Base>&)
Is there a general rule that I am violating here?
Tim
#include <iostream>
#include <string>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
class Base {
public:
virtual ~Base() {}
virtual string evaluate() const = 0;
};
typedef shared_ptr<Base Base_ptr;
//
// Test 1
//
// define Derived1 as a global type
class Derived1: public Base {
public:
Derived1(): Base() {}
virtual ~Derived1() {}
virtual string evaluate() const { return "Derived1"; }
};
void
test1()
{
// Base_ptr to subclass
Base_ptr base_der(new Derived1);
}
//
// Test 2
//
void
test2()
{
// define Derived2 as a local type
class Derived2: public Base {
public:
Derived2(): Base() {}
virtual ~Derived2() {}
virtual string evaluate() const { return "Derived2"; }
};
Base_ptr base_der(new Derived2);
}
int
main()
{
test1();
test2();
return 0;
} 3 2745
On Mar 13, 9:22 pm, "Tim H" <thoc...@gmail. comwrote:
I'm newish to C++ but not to C.
I'm confused by this code. test1() is fine. test2() fails to
compile.
/tmp/inherit_ptr.cpp : In function âvoid test2()â:
/tmp/inherit_ptr.cpp :52: error: no matching function for call to
âboost::shared_ ptr<Base>::shar ed_ptr(test2(): :Derived2*)â
/usr/include/boost/shared_ptr.hpp: 119: note: candidates are:
boost::shared_p tr<T>::shared_p tr() [with T = Base]
/usr/include/boost/shared_ptr.hpp: 106: note:
boost::shared_p tr<Base>::share d_ptr(const boost::shared_p tr<Base>&)
Is there a general rule that I am violating here?
Yes - local types can't be used as template arguments (something do do
with having no linkage? I'm sure someone will correct / elaborate on
this).
I have never seen local class definitions in "real" code, and would
hesitate to use them because a) they're exotic and potentially
confusing, and b) the limitation you just ran into rather limits their
usefulness. Could you consider using an anonymous namespace instead?
On Mar 13, 5:07 pm, "Pete C" <5gv7rq...@snea kemail.comwrote :
On Mar 13, 9:22 pm, "Tim H" <thoc...@gmail. comwrote:
I'm newish to C++ but not to C.
I'm confused by this code. test1() is fine. test2() fails to
compile.
/tmp/inherit_ptr.cpp : In function âvoid test2()â:
/tmp/inherit_ptr.cpp :52: error: no matching function for call to
âboost::shared_ ptr<Base>::shar ed_ptr(test2(): :Derived2*)â
/usr/include/boost/shared_ptr.hpp: 119: note: candidates are:
boost::shared_p tr<T>::shared_p tr() [with T = Base]
/usr/include/boost/shared_ptr.hpp: 106: note:
boost::shared_p tr<Base>::share d_ptr(const boost::shared_p tr<Base>&)
Is there a general rule that I am violating here?
Yes - local types can't be used as template arguments (something do do
with having no linkage? I'm sure someone will correct / elaborate on
this).
I have never seen local class definitions in "real" code, and would
hesitate to use them because a) they're exotic and potentially
confusing, and b) the limitation you just ran into rather limits their
usefulness. Could you consider using an anonymous namespace instead?
The example arose as part of a unit-test, where the Derived2 class was
defined as part of the test function.
Tim
Pete C <5g*******@snea kemail.comwrote :
On Mar 13, 9:22 pm, "Tim H" <thoc...@gmail. comwrote:
>I'm newish to C++ but not to C.
I'm confused by this code. test1() is fine. test2() fails to compile.
/tmp/inherit_ptr.cpp : In function âvoid test2()â: /tmp/inherit_ptr.cpp :52: error: no matching function for call to âboost::shared _ptr<Base>::sha red_ptr(test2() ::Derived2*)â /usr/include/boost/shared_ptr.hpp: 119: note: candidates are: boost::shared_ ptr<T>::shared_ ptr() [with T = Base] /usr/include/boost/shared_ptr.hpp: 106: note: boost::shared_ ptr<Base>::shar ed_ptr(const boost::shared_p tr<Base>&)
Is there a general rule that I am violating here?
Yes - local types can't be used as template arguments (something do do
with having no linkage? I'm sure someone will correct / elaborate on
this).
Yes, I think the term is "external linkage", which local types do not
have.
I have never seen local class definitions in "real" code, and would
hesitate to use them because a) they're exotic and potentially
confusing, and b) the limitation you just ran into rather limits their
usefulness.
There have been several times when I wanted to declare a local/nested
class that served as a utility for the outer class but had no use
outside of this class; however, I could not use any of the <algorithm>s
or std containers on them because of the above reason. If I could use
templates with local classes, they would make my code slightly cleaner.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: SerGioGio |
last post by:
Hello !
When a ref. counted smart pointer (template) class is available (like
boost::shared_ptr), is there any (semantic) reason to still use
std::auto_ptr rather than this smart pointer ? Or should one use shared_ptr
for every cases ?
Thanks in advance for your remarks !
SerGioGio
|
by: Ryan Mitchley |
last post by:
Hi all
Given
bool bResult;
shared_ptr<cSampleData> pNewData;
shared_ptr<cBase> pNewBase;
where cSampleData is descended from cBase, the following gives me a valid
pNewData to the correct type:
|
by: Sean |
last post by:
I have a boost::shared_ptr and want to detach the primitive pointer
within it so I can keep using it even after the shared_ptr is
destroyed.
Is there a way to do this?
|
by: adebaene |
last post by:
Hello group,
There seems to be a bug int the interop layer in VC2005 when dealing
with certain pointer types (or values?)
Here is a repro case using Boost version 1.32 and C++/CLI :
using namespace System;
#pragma unmanaged
|
by: Alan Johnson |
last post by:
From the standard 5.3.5/5:
"If the object being deleted has incomplete class type at the point of
deletion and the complete class has a non-trivial destructor or a
deallocation function, the behavior is undefined."
In the program below, what constitutes "point of deletion"? Presumably,
since delete is called somewhere within shared_ptr's template, the point
of deletion is the same place at which the template is instantiated.
Does the...
| |
by: g |
last post by:
Hello,
Wouldn't be better shared_ptr constructor to have an argument
<true/falsefor thread-safe reference counting?? The default should be
true and in case you dont want thread-safe RC create a shared_ptr with
false in constructor. The reason for this is to avoid the cost of the
memmory barriers where you don't need them.
At the moment if your application is multithreaded boost will choose to
use thread-safe RC but in many cases(...
|
by: hsmit.home |
last post by:
Hello,
I came across a strange error and it's really been bugging me. Maybe
someone else has come across this and any insight would be
appreciated.
What I'm trying to accomplish is using boost::lexical_cast to cast a
vector of strings to a string.
The compiler I'm using is MSVC++ 2005 Express Edition. The gc++
|
by: mike.polyakov |
last post by:
I have trouble understanding incomplete types and their interplay with
shared_ptr. Consider the following code, composed of two source files
and one header:
//------------------------------------------------------------------
// test.h
#ifndef TEST_H_
#define TEST_H_
#include <boost/shared_ptr.hpp>
using namespace boost;
|
by: er |
last post by:
Hi All, could anyone make a suggestion to fix the code below? Thanks!
class A{
public:
/* constructor */
double value()const{/* implementation */};
};
typedef std::vector<boost::shared_ptr<A dataset_type;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |