Help with this basic code
-
hi, i am having some trouble with this basic example code, i can't figure out why i am getting an error, null reference! please help thanks
#include "stdafx.h" #using mscorlib.dll using namespace std; class Foo { public: Foo(); ~Foo(); void DoSomeFoo(); }; // constructor Foo::Foo() { cout<<"Constructor called!"<DoSomeFoo(); } int _tmain() { MFoo*myfoo; myfoo->ManagedDoSomeFoo(); return 0; }
-
hi, i am having some trouble with this basic example code, i can't figure out why i am getting an error, null reference! please help thanks
#include "stdafx.h" #using mscorlib.dll using namespace std; class Foo { public: Foo(); ~Foo(); void DoSomeFoo(); }; // constructor Foo::Foo() { cout<<"Constructor called!"<DoSomeFoo(); } int _tmain() { MFoo*myfoo; myfoo->ManagedDoSomeFoo(); return 0; }
-
hi, i am having some trouble with this basic example code, i can't figure out why i am getting an error, null reference! please help thanks
#include "stdafx.h" #using mscorlib.dll using namespace std; class Foo { public: Foo(); ~Foo(); void DoSomeFoo(); }; // constructor Foo::Foo() { cout<<"Constructor called!"<DoSomeFoo(); } int _tmain() { MFoo*myfoo; myfoo->ManagedDoSomeFoo(); return 0; }
Your never actually creating an instance of
myfoo
, try changing your_tmain
to:int _tmain()
{
MFoo*myfoo = new MFoo( ); // Create an instance of this object
myfoo->ManagedDoSomeFoo();
return 0;
}Gavin Taylor w: http://www.gavspace.com -- modified at 5:39 Wednesday 8th March, 2006
-
hi, i am having some trouble with this basic example code, i can't figure out why i am getting an error, null reference! please help thanks
#include "stdafx.h" #using mscorlib.dll using namespace std; class Foo { public: Foo(); ~Foo(); void DoSomeFoo(); }; // constructor Foo::Foo() { cout<<"Constructor called!"<DoSomeFoo(); } int _tmain() { MFoo*myfoo; myfoo->ManagedDoSomeFoo(); return 0; }
thanks guys, i thought it was something like that,but i mainly use vb.net so i get a little stuck with c++. i have been trying to wrap this example, so i can practice for a much bigger program. i have never done this before and the help on the net is a little vague, i need a very very basic walkthrough, because the ones i have found won't compile, and its hard enough trying to debug code that you dont really understand. i know this much.... i have a class library(.net) called fooManaged and its automatical created fooManaged.h. and i created UnManaged.h in the UnManaged.h file i have this code.....
#include iostream #include vcclr.h using namespace std; class Foo { public: Foo(); ~Foo(); void DoSomeFoo(); }; // constructor Foo::Foo() { cout<<"Constructor called!"< in fooManaged.h i have this code.... `// fooManaged.h #pragma once using namespace System; namespace fooManaged { public __gc class MFoo { private: Foo * _foo; public: MFoo(); ~MFoo(); void ManagedDoSomeFoo(); }; // constructor MFoo::MFoo() { _foo = new Foo(); } // destructor MFoo::~MFoo() { delete _foo; } // method void MFoo::ManagedDoSomeFoo() { _foo->DoSomeFoo(); } }` the fooManaged.cpp file just has... #include "stdafx.h" #include "Unmanagedfoo.h" #include "fooManaged.h" #using mscorlib.dll it all compiles...but there is a big linking error!! Compiling... fooManaged.cpp Linking... LINK : error LNK2020: unresolved token (0A000005) _CxxThrowException LINK : error LNK2020: unresolved token (0A000019) free LINK : error LNK2020: unresolved token (0A00001A) strlen LINK : error LNK2020: unresolved token (0A00001B) memmove LINK : error LNK2020: unresolved token (0A00001C) memcpy LINK : error LNK2020: unresolved token (0A00001D) delete LINK : fatal error LNK1120: 6 unresolved externals i must be doing something wrong, some help with this will be very wecome thanks