While writing code I observed that if i declare unmanaged classs before managed class the code compiles with no error:
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace System;
namespace OpenCVDll
{
public class OpenCV
{
public:
//members
cv::Mat CalibrationDark;
cv::Mat CalibrationBright;
unsigned short* dark;
unsigned short* bright;
//methods
void DarkCalibration();
void BrightCalibration();
OpenCV(){}
~OpenCV();
};
public ref class MOpenCV
{
public:
//members
OpenCV* UOpenCV;
//methods
MOpenCV();
!MOpenCV();
~MOpenCV();
private:
//methods
void Destruction();
};
}`
However if I declare the classes other way around, first managed and then unmanaged:
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace System;
namespace OpenCVDll
{
public ref class MOpenCV
{
public:
//members
OpenCV* UOpenCV;
//methods
MOpenCV();
!MOpenCV();
~MOpenCV();
private:
//methods
void Destruction();
};
public class OpenCV
{
public:
//members
cv::Mat CalibrationDark;
cv::Mat CalibrationBright;
unsigned short* dark;
unsigned short* bright;
//methods
void DarkCalibration();
void BrightCalibration();
OpenCV(){}
~OpenCV();
};
}
I get missing type error: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Why is it so?
The problem is that when the compiler gets to the line OpenCV* UOpenCV;, the class OpenCV has not been declared yet.
You can solve this with a forward declaration of the OpenCV class before the MOpenCV class.
public class OpenCV;
public ref class MOpenCV
{
public:
//members
OpenCV* UOpenCV;
...
};
public class OpenCV
{
...
};
Related
I am converting a project from /oldsyntax to /clr and have problems to convert my properties in the public __gc class Reader which has now become public ref class Reader
I have these properties (amongst others) in the .h file
__property void set_Xml(System::String *value);
__property System::String *get_Xml();
and then in the .cpp file I have
void Reader::set_Xml(System::String *value)
{
if(value->Chars[0] == '<'){
reader->put_xml(stlString(value).c_str());
}
else {
reader->put_xml_file(stlString(value).c_str());
}
}
System::String *Reader::get_Xml()
{
return gcString(reader->get_xml(), reader->state.is_utf8);
}
How do I rewrite this so that it can compile with /clr. I am using Visual Studio 2010 ?
The link posted in the comments has all the information on the new syntax for properties.
Old: Declare methods with a specific naming convention, decorate them with __property.
New: Declare a property block within your class, and have methods with an additional level of scope. (Note: I'm not sure if "additional level of scope" is the proper way to describe it, see below.)
For a property named Xml of type String, the syntax would be:
In the header file:
public ref class Reader
{
public:
property String^ Xml
{
String^ get();
void set(String^ value);
}
}
In the .cpp file:
String^ Reader::Xml::get()
{
return whatever;
}
void Reader::Xml::set(String^ value)
{
whatever = value;
}
I am just making a simple example of inheritance in C++. I am using Xcode and whenever I create a subclass I obtain the error: Use of undeclared identifier Rat. These are my classes:
Pet.h
#include <iostream>
#include <string>
using namespace std;
class Pet
{
public:
// Constructors, Destructors
Pet(): weight(1), food("Pet Chow") {}
~Pet() {}
//General methods
void eat();
void speak();
protected:
int weight;
string food;
};
Rat.h
#include <iostream>
#include "Pet.h"
using namespace std;
class Rat::public Pet
{
Rat() {}
~Rat() {}
// Other methods
void sicken() { cout << "Spreading plague" << endl; }
}
I think you mean
class Rat : public Pet
class Rat::public Pet
should be
class Rat: public Pet
I am using VS2010, c++ windows forms application.
Here is my class
#pragma once
public ref class FrameMatrix
{
public:
FrameMatrix(void);
FrameMatrix(int IdNm, int ClmnAmnt, int RwsAmnt);
String^ FMName;
int FMIdentificationNumber;
array<array<double>^>^ FMArray;
};
Now I try to put it into Dictionary as value.
And I get unresolved tokens.
#pragma once
#include "FrameMatrix.h"
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Dictionary<int, FrameMatrix^>^ ImportedData;
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
ImportedData = gcnew Dictionary<int, FrameMatrix^>;
}
You forgot the parentheses:
ImportedData = gcnew Dictionary<int, FrameMatrix^>();
^^
If that doesn't fix the error, please post the exact error message you're getting.
I have a class like this:
public ref class Test
{
public:
property int MyProperty;
};
This works. Now I want to move the implementation of MyProperty to the CPP file. I get compiler errors that the property is already defined when I do this:
int Test::MyProperty::get() { return 0; }
What is the proper syntax for this?
In the header change the declaration to:
public ref class Test
{
public:
property int MyProperty
{
int get();
void set( int );
}
private:
int m_myProperty;
};
Then, in the cpp code file write your definitions like this:
int Test::MyProperty::get()
{
return m_myProperty;
}
void Test::MyProperty::set( int i )
{
m_myProperty = i;
}
The reason you are seeing errors is that you have declared a trivial property where the compiler generates an implementation for you. But, then you tried to provide an implementation explicitly as well. See: http://msdn.microsoft.com/en-us/library/windows/apps/hh755807(v=vs.110).aspx
Most of the examples online only show implementations directly in the class definition.
In the class definition, you need to declare the property as a property with user-declared get and set methods; it cannot be a shorthand property:
public ref class Test
{
public:
property int MyProperty { int get(); void set(int); }
};
Then in the cpp file you may define the get() and set() methods:
int Test::MyProperty::get()
{
return 42;
}
void Test::MyProperty::set(int)
{
// set the value
}
Say I have a C# interface called IMyInterface defined as follows:
// C# code
public interface IMyInterface
{
void Foo(string value);
string MyProperty { get; }
}
Assume I also have a C++/CLI class, MyConcreteClass, that implements this interface and whose header is declared as follows:
// C++/CLI header file
ref class MyConcreteClass : IMyInterface
{
public:
};
How does one implement the method Foo and the property MyProperty in the C++/CLI header?
My attempt results in the following compile error:
error C3766: 'MyConcreteClass' must
provide an implementation for the
interface method 'void
IMyInterface::Foo(System::String^
value)'
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual void __clrcall Foo(String^ value) sealed;
virtual property String^ __clrcall MyProperty
{ String^ get() sealed { String::Empty; } }
};
Interfaces need to be defined as virtual. Also note the "public IMy.." after the class decleration, it's a slighly different syntax than C#.
If you can, seal the interface members to improve performance, the compiler will be able to bind these methods more tightly than a typical virtual members.
Hope that helps ;)
I did not compile it but looks good to me... Oh and also, defining your methods as __clrcall eliminates dangers of double thunk performance penalties.
edit
the correct syntax for a property is:
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual property String^ MyProperty
{
String^ get() sealed { return String::Empty; };
void set( String^ s ) sealed { };
}
};
or, when putting the definition in the source file:
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual property String^ MyProperty
{
String^ get() sealed;
void set( String^ s ) sealed;
}
};
String^ MyConcreteClass::MyProperty::get()
{
return String::Empty;
}
void MyConcreteClass::MyProperty::set( String^ )
{
//...
}