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;.
Related
I have a C++ class in my application testclient:
namespace testclient{
namespace models{
ref class myclass sealed{
public:
myclass();
property String^ getstring
{
String^ get()
{
return string;
}
}
private:
String^ string = "test";
}}}
I want to bind a control to the property getstring, and from what little I understand of UWP XAML data binding, I have to include this in the top of the MainPage.xaml: xmlns:data="using:testclient.models Problem is, intellisense is telling me "Undefined namespace. The 'using' URI refers to a namespace called testclient.models that could not be found." What am I doing wrong?
EDIT: I've found the problem goes away when I put the class in Mainpage.Xaml.h, but I'd rather not do this...
Every binding consists of a binding target and a binding source. Typically, the target is a property of a control or other UI element, and the source is a property of a class instance.
If you want to use myclass as datasource to MainPage's UI elements, you need to make sure the instance of the myclass is accessible to MainPage. That's why your first version resulted in error. In order to modify mainPage.Xaml.h as little as possible, you could follow steps below by creating a separate file(I simplified the member of myclass for easy debugging):
1) Create myclass.h:
namespace TestClient{
namespace models{
public ref class myclass sealed
{
private:
int test = 1;
public:
myclass()
{
}
property int gettest
{
int get() { return test; };
}
};
}
}
2) in MainPage.h, add following:
#include "myclass.h"
namespace TestClient
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public ref class MainPage sealed
{
private:
TestClient::models::myclass myTest;
.......
}
.........
}
3) Then you can manipulate myclass data in mainPage.cpp as you want. Codes may be like below:
MainPage::MainPage()
{
InitializeComponent();
int i = this->myTest.gettest;
...........
}
Still I have a question: while so many namespace nested? Also you can find a sample about data binding here just for your reference.
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
{
...
};
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 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.