|
Hello everyone,
Two questions,
1.
The following compiler error from the following code segment, is because delete statement from destructor of auto_ptr requires the declaration of destructor of wrapped object?
1>main.cpp
1>d:\visual studio 2008\projects\test_autoptr2\test_autoptr2\main.cpp(10) : error C2512: 'Foo' : no appropriate default constructor available
#include <memory>
#include <iostream>
using namespace std;
class Foo;
void func()
{
auto_ptr<Foo> pf (new Foo());
cout << "I am here. " << endl;
return;
}
int main()
{
func();
return 0;
}
class Foo {
};
2.
If yes, how to define a destructor outside of the body of class declaration? I defined in this way, but compile can not pass.
Foo::~Foo()
{
}
thanks in advance,
George
|
|
|
|
|
George_George wrote: 1.
The following compiler error from the following code segment, is because delete statement from destructor of auto_ptr requires the declaration of destructor of wrapped object?
the compiler error statement shows error on costructor and why you suspect the destructor.
auto_ptr<Foo> pf (new Foo()); and the eror is not related to autoptr, it is pointing to the statement with in it, "new Foo()" this requires the declaration of Foo to be visible.
And why do you given a forward declaration of class Foo.
|
|
|
|
|
Thanks Rajkumar,
I have found this issue, we should declare (not necessary to implement) constructor and destructor before using new to create the object, right?
regards,
George
|
|
|
|
|
George_George wrote: we should declare (not necessary to implement) constructor and destructor before using new to create the object, right?
yes, that holds for not only constructor and destructor but any member.
|
|
|
|
|
Thanks Rajkumar,
Any other member you mean like default assignment operator?
Rajkumar R wrote: but any member
regards,
George
|
|
|
|
|
I mean if you are accessing any member, member data, member function you need declaration, forward declaration is not sufficient.
|
|
|
|
|
No Rajkumar,
I do not agree with you. I think forward declaration is enough,
here is my code to prove, any comments?
#include <memory>
#include <iostream>
using namespace std;
class Foo {
public:
Foo();
~Foo();
};
void func()
{
auto_ptr<Foo> pf (new Foo());
cout << "I am here. " << endl;
return;
}
int main()
{
func();
return 0;
}
Foo::Foo()
{
}
Foo::~Foo()
{
}
regards,
George
|
|
|
|
|
I don't think this is the proof of concept code. AFAIK, this code has the full declaration of class Foo before it is referenced in func(), compared to the previous where you did class forward declaration "class Foo; only"
Try the following as in your previous post.
using namespace std;
class Foo;
void func()
{
auto_ptr<Foo> pf (new Foo());
cout << "I am here. " << endl;
return;
}
int main()
{
func();
return 0;
}
class Foo
{
public:
Foo();
~Foo();
};
Foo::Foo()
{}
Foo::~Foo()
{}
i did a quick search now this link may gives some idea on Class Forward Declaration When Full Declarations Are Not Required[^]
modified on Friday, March 21, 2008 8:50 AM
|
|
|
|
|
Cool, Rajkumar!
Question answered.
regards,
George
|
|
|
|
|
hello all:
i have a prolem to ask
i want to creat a dll
in this dll i hava a class i want to export the function of the class
whether i can invoke the export function of the dll using c lanuage programmer?
a beginner
|
|
|
|
|
See here.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Broken link alert
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
thanks all
i got the method
I use the class in a global function
then i deifne the global function as export function
a beginner
|
|
|
|
|
alphaxz wrote: I use the class in a global function
then i deifne the global function as export function
If that's what you want. That's not the same as exporting a
function of a class. That's like using C++ without classes - C.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
WOW, C++ classes encapsulation with C global functions. Is that the C-nemesis pattern?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
We're not supposed to mention the C-nemesis pattern, are we?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It depends on the mood of the day.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Are you sure? It should go to James' post below.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I must have scrolled down too soon, before the post opened - sorry
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I prefer something like this, using __declspec(dllexport)/__declspec(dllimport)...
In a header file shared by both the DLL and other modules:
#if defined(BUILDING_MY_DLL)
#define MY_DLL_IMP_EXP __declspec(dllexport)
#else
#define MY_DLL_IMP_EXP __declspec(dllimport)
#endif
class MyClass
{
public:
MY_DLL_IMP_EXP int SomeFunc();
};
In the DLL code:
#include "MyClass.h"
int MyClass::SomeFunc()
{
return 12;
}
In the module importing the function
#include "MyClass.h"
MyClass mc;
int ret = mc.SomeFunc();
Add a define compiler option for BUILDING_MY_DLL (/D "BUILDING_MY_DLL") on the DLL project settings,
but not on any projects that use the DLL.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
This compiles on NET2003 but doesn't on NET 2005or 2008
typedef float floatArray __gc[];
public: __event void Mcs600_RawDataRDY( floatArray sampleSpectrum);
Error 1 error C2691: 'Mcs600CTL::Mcs600CTLControl::floatArray' : a managed array cannot have this element type c:\work\net2005\mcs600ctl\Mcs600CTLControl.h 143
__raise Mcs600_RawDataRDY(sampleSpectrum);
Error 2 error C2664: 'Mcs600CTL::Mcs600CTLControl::raise_Mcs600_RawDataRDY' : cannot convert parameter 1 from 'float __gc[]' to 'Mcs600CTL::Mcs600CTLControl::floatArray __gc[]' c:\work\net2005\mcs600ctl\Mcs600CTLControl.h 577
|
|
|
|
|
I'm not sure what you're trying to do there.
Are you still using the old managed extensions syntax?
Mark
p.s. This is the wrong board - this should be on the C++/CLI board
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark:
I'm trying to pass a floating point vector back to the caller by raising an Event. I wasn't aware that there were new extensions syntax. I thought I had published it to the C++ board.
Thanks
Dan
|
|
|
|
|
Member 2021022 wrote: I wasn't aware that there were new extensions syntax
Starting with 2005 there's no more "managed extensions" to C++.
It's now formally C++/CLI. You can still build with the old pre-VS 2005 syntax by
setting a compiler setting, but that setting is deprecated and the new syntax is
a lot nicer anyway.
Under the new syntax, you could do something like this:
typedef array<float> floatArray;
...
floatArray ^farray = gcnew floatArray(10);
Member 2021022 wrote: I thought I had published it to the C++ board
You did, but there's a managed C++/CLI board as well
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|