Can someone explain me what am I doing wrong here?
I have two classes and I have to access the value of class A variables into Class B and then compare them and this is what I am doing and I am unable to access the variable
public ref class A : public System::Windows::Forms::Form
{
public:
String^ name;
String^ address;
private: System:: void foo()
{
name=textBox1->Text;
address=textBox2->Text;
}
}`
public ref class B : public System::Windows::Forms::Form
{
public:
String^ name1;
String^ address1;
A^ obj;
private: void foo()
{
MessageBox::show(obj->name);
MessageBox::show(obj->address);
}
}`
This gives me an error saying syntax error on line A^ obj; when I have one and saying missing type specifier both these classes are in a different header files
In an attempt to be helpful, and i will probably get some down votes... haters be nice.
This is C++/CLI, so asking about just C++ will get you no help as that is a very special type of C++ that i will not attempt to explain. Gordon Hogenson has written an excellent book on the subject.
Class A has a Class B
However, you show nothing that would set name and address as part of foo() being executed
If you are suggesting this is a compile error, this compiles for me
public ref class A : public System::Windows::Forms::Form
{
public:
String^ name;
String^ address;
private: void foo()
{
name = "Test this";
address ="Test 2";
}
};
public ref class B : public System::Windows::Forms::Form
{
public:
String^ name1;
String^ address1;
A^ obj;
private: void foo()
{
MessageBox::Show(obj->name);
MessageBox::Show(obj->address);
}
};
I do not think it will do what you expect, but that is a different issue if you are battling a compile time error. To address the compile time issue, provide the real .h and .cpp and not a hybrid that you think is the problem.
Regarding the setting of A within B, this is a variable scope issue of OO and is independent discussion of compile time
Related
class factory final {
private:
class object final {
public:
int x;
};
public:
inline static std::shared_ptr<object> createObject() { return std::make_shared<object>(); }
};
int main() {
auto item1 = factory::createObject();
std::shared_ptr<factory::object> item2 = factory::createObject();
return std::getchar();
}
Hello. The code above fails to compile with VS2015 because I cannot access private member (class object) of class factory. So far so good and it makes alot of sense. I am curious why does the auto line works. It does resolve to correct type and works as intended.
I am trying to use QTConcurrent class to launch some tasks asynchronously but I am getting some errors:
This is my code:
class A {
public:
void method1();
};
class B {
std::unique_ptr<A> ptr;
public:
void method2() {
QtConcurrent::run(&this->ptr, &A::method1);
}
}
I get compilation error.
Could someone tell me what the correct syntax is?
Thanks in advance and regards
I finally was able to find working version:
class A {
public:
void method1();
};
class B {
std::unique_ptr<A> ptr;
public:
void method2() {
QtConcurrent::run(this->ptr.get(), &A::method1);
}
}
I know that namespace are preferred in C++ if you want to define static functions, but what to do when you have functions which access other functions but the other functions should be private and not be accessible by others. How to declare/define it ? Wouldn't it be much simple to use classes ? Or do I have to still use namespaces ?
Like this:
class Test1
{
public:
static void Start()
{
if(1) // Some checks...
{
ProcessStart();
}
}
private:
static void ProcessStart()
{
if(!Initialized)
{
//Initialize
}
// Do other stuff
}
static bool Initialized;
};
bool Test1::Initialized = false;
You don't need to expose private details in the public API:
foo.hpp:
#ifndef H_FOO
#define H_FOO
namespace Foo
{
void Start();
}
#endif
foo.cpp:
#include "foo.hpp"
namespace Foo
{
namespace
{
void ProcessStart()
{
// ...
}
}
void Start()
{
ProcessStart();
}
}
Only the header file ends up in your public /include directory, so nobody will ever know that there are private implementation functions (except perhaps your debugger).
This is a class library clr/c++ project.
Class A is unmanaged c++, class B managed c++.
I would like to create an object of B from a C# application and call the "void Sign" with that object and catch the StatusEvent in C#.
How to call B::Showsts from A::call_A in order to achieve this? Please keep in mind that call_A is called from a delegate of the B class object.
Thank you in advance!
public class A{
public:
int call_A();
};
public ref class B{
private:
A* a1;
public:
void Sign(String^ ufile);
void Showsts(string sts);
delegate void GetResult(String^);
event GetResult^ StatusEvent;
SpyrusLib(void){
a1=new A();
}
protected: ~SpyrusLib(){
delete a1;
}
private:
String^ str;
delegate int MySignDelegate(String^);
int MySign(String^ file);
void Callbacksign(IAsyncResult ^ar);
};
void B::Sign(String^ ufile){
MySignDelegate^ signDel = gcnew MySignDelegate( this, &B::MySign );
AsyncCallback^ cb = gcnew AsyncCallback( this, &B::Callbacksign);
signDel->BeginInvoke( ufile , cb, signDel );
}
int B::MySign(String^ file){
stdstr=msclr::interop::marshal_as<std::string>(file);
a1->call_A(stdstr);
}
void B::Showsts(string sts){
str = gcnew String(sts.c_str());
StatusEvent(str);
}
int A::call_A(string stat){
?-Showsts(stat);
}
I'm not sure it's the best solution but I solved it adding the following things to the classes:
typedef void (__stdcall * Unmanagedstatus)(string sts);
using namespace std;
public class A{
private:
Unmanagedstatus sendmsg;
public:
int call_A();
spyrus(Unmanagedstatus unm)
{
sendmsg=unm;
}
};
public ref class B
{
private:
delegate void Managedstatus(string);
Managedstatus^ managed;
IntPtr unmanaged;
A* a1;
public:
SpyrusLib(void)
{
managed = gcnew Managedstatus(this, &B::Showsts);
unmanaged = Marshal::GetFunctionPointerForDelegate(managed);
a1=new A((Unmanagedstatus)(void*)unmanaged);
}
}
int A::call_A(string stat){
sendmsg(stat); // this will call B::Showsts and the events raised
//from Showsts are also working in the C# app
}
I have an interface, and I was trying an example on dynamic polymorphism as follows:
#include <iostream>
using namespace std;
class foo{
public:
virtual void set();
virtual void printValue();
};
class fooInt : public foo{
private:
int i;
public:
int get(){
return i;
}
void set(int val){ //override the set
i = val;
}
void printValue(){
cout << i << endl;
}
};
int main(){
foo *dt; //Create a base class pointer
dt = new fooInt; //Assign a sub class reference
dt->set(9);
}
However when I compile this, I get no matching function for call to ‘foo::set(int)’. Where am I going wrong? I tried to read this article, and I still couldn't figure out the mistake.
class foo has no method set(int). It has a method set(), but no method set(int).
If you intend to override an inherited method, the superclass method and your method must have the same signature:
class foo {
...
// If you really want an abstract class, the `= 0`
// ensures no instances can be created (makes it "pure virtual")
virtual void set(int) = 0;
...
}
This is because your definition of
virtual void set();
Should be
virtual void set(int val);
The corrected program is given here
#include <iostream>
using namespace std;
class foo {
public:
virtual void set(int val)=0;////////here you have void set() function with no argument but you tried to override void set(int val) which take one argument.
virtual void printValue()=0;
};
class fooInt : public foo{
private:
int i;
public:
fooInt()
{
cout<<"constructor called\n";
}
int get(){
return i;
}
void set(int val){ //override the set
i = val;
}
void printValue(){
cout << i << endl;
}
};
int main(){
foo *dt; //Create a base class pointer
dt=new fooInt;
dt->set(9);
dt->printValue();
}
Fault of the previous program were
1.You tried to override set() {no argument} with set(int val){one argument}.
2.When a class contain a pure virtual function,it must be implemented by its derived classes.
3. No object can be created of a class which contain a pure virtual function.But ref can be created.
Thanks