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
Related
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
{
...
};
When i run this program it says in this file there is error called error C2653: 'Application' : is not a class or namespace name
#include "MyForm.h"
using namespace std;
int main() {
public:
static void main1() {
// Starts the application.
Application::Run(gcnew MyForm());
}
}
You seem to be missing using namespace System::Windows::Forms;.
I have following code; I want to convert it for winRT. Actually, I dont know how to handle ISerializable, Serializable, SerializationInfo and COMPACT_FRAMEWORK
using System;
using System.Collections;
#if !COMPACT_FRAMEWORK
using System.Runtime.Serialization;
#endif
namespace Coversant.Attributes
{
[Serializable]
public class AssertionFailedError : Exception
#if !COMPACT_FRAMEWORK, ISerializable
#endif
{
#if !COMPACT_FRAMEWORK
protected AssertionFailedError(SerializationInfo info, StreamingContext context) : base(info, context){}
#endif
}
}
Well, COMPACT_FRAMEWORK is I believe what you would have on some old, small devices and the preprocessor directives (#if, #endif) simply delimit code that should be used when the code is compiled when building for anything but Compact Framework. WinRT is actually similar in having these missing, but also has the Serializable attribute missing so you would do something like this, which is essentially a simple Exception class definition that doesn't include any new or overridden members:
using System;
using System.Collections;
#if (!COMPACT_FRAMEWORK && !NETFX_CORE)
using System.Runtime.Serialization;
#endif
namespace Coversant.Attributes
{
#if !NETFX_CORE
[Serializable]
#endif
public class AssertionFailedError : Exception
#if (!COMPACT_FRAMEWORK && !NETFX_CORE)
, ISerializable
#endif
{
#if (!COMPACT_FRAMEWORK && !NETFX_CORE)
protected AssertionFailedError(SerializationInfo info, StreamingContext context) : base(info, context){}
#endif
}
}
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.
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^ )
{
//...
}