c++/cli error C2143: syntax error : missing ';' before '.' - c++-cli

High, guys! Sorry to ask you this question, but I canĀ“t find what causes the error. Also, I'm new at Visual C++/CLI, so I know my code could use some polishing.
In short, what I'm trying to do is capture data from a form to build a class.
I'd appreciate any help. THANKS IN ADVANCE.
My code:
// Form4A.h
#pragma once
# include "Tutors.h"
namespace SisPro
{
//.... more code
public ref class Form4A : public System::Windows::Forms::Form
{
#pragma region Windows Form Designer generated code
//.... more code
#pragma endregion
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^ m1 = comboBox14->SelectedItem->ToString();
String ^ m2 = comboBox19->SelectedItem->ToString();
String ^ m3 = comboBox20->SelectedItem->ToString();
Tutors.add_tutor(m1, m2, m3);// ERROR C2143
}
};
}
SOMWHERE ELSE:
//Tutors.h
using namespace System;
public ref class Tutors
{
public:
Tutors();
void add_tutor(String ^ m1, String ^ m2, String ^ m3)
private:
String ^ ID;
String ^ LASTNAME;
String ^ NAME;
};
// tutors.cpp
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "Tutores.h"
Tutors::Tutors()
{
ID = "";
LASTNAME = "";
NAME = "";
}
void Tutors::add_tutor(String ^ m1, String ^ m2, String ^ m3)
{
ID = m1;
LASTNAME = m2;
NAME = m3;
return;
}

Add field Tutors in class Form4A. And add field initialization in constructor.
public ref class Form4A : public System::Windows::Forms::Form
{
#pragma region Windows Form Designer generated code
//.... more code
Form4A()
{
//..
this->Tutors = gcnew Tutors();
}
#pragma endregion
Tutors^ Tutors;
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^ m1 = comboBox14->SelectedItem->ToString();
String ^ m2 = comboBox19->SelectedItem->ToString();
String ^ m3 = comboBox20->SelectedItem->ToString();
Tutors.add_tutor(m1, m2, m3);// ERROR C2143
}
};

Related

In C++/CLI what is the easiest way to pass internal variables to delegate?

I am trying to set a delegate that takes a local variable as a parameter. The declaration looks like this:
ref class Main
{
private:
Func<String^>^ _expensiveMethodDelegate;
public:
property Func<String^>^ ExpensiveMethodDelegate
{
Func<String^>^ get() { return this->_expensiveMethodDelegate; }
void set(Func<String^>^ value) { this->_expensiveMethodDelegate = value; }
};
void DoWork()
{
String^ result = this->_expensiveMethodDelegate();
Debug::WriteLine(result);
}
};
In C# the code will look like this:
string parameter = "value";
Main main = new Main();
main.ExpensiveMethodDelegate = () =>
{
Thread.Sleep(1000); // do expensive work
return parameter + "1";
};
main.DoWork();
What is the easiest way of achieving this goal using managed C++ (VS 2015)? Note: I read articles Workaround for not having lambdas that can capture managed variables and Lambda expressions as CLR (.NET) delegates / event handlers in Visual C++ 2010 and still cannot figure out what is the solution.
I tried code like this (using make_delegate from the second article), but it fails to compile:
String^ parameter = L"value";
Main^ main = gcnew Main();
main->ExpensiveMethodDelegate = make_delegate(
[](String^ parameter) -> String^
{
Threading::Thread::Sleep(1000); // do work
return parameter + L"1";
});
main->DoWork();
This is what I came up with:
#pragma once
#include <new>
using namespace std::tr1;
using namespace System;
namespace helper
{
private struct return_type_helper
{
private:
template<class D>
struct dependent_false { enum { value = false }; };
template <class D>
struct illegal_delegate_type
{
static_assert(dependent_false<D>::value, "Delegates with more than 2 parameters, or with parameters of tracking reference types (T%), are not supported.");
};
struct anything
{
template<class T>
operator T() const;
};
public:
template<class D>
static decltype(static_cast<D^>(nullptr)(anything())) dummy(int(*)[2]);
template<class D>
static decltype(static_cast<D^>(nullptr)(anything(), anything())) dummy(int(*)[3]);
};
template<class Func, class Aligner = char, bool Match = (alignment_of<Func>::value == alignment_of<Aligner>::value)>
struct aligner
{
static_assert(Match, "Function object has unsupported alignment");
};
template<class Func, class Aligner>
private struct aligner<Func, Aligner, true>
{
typedef Aligner type;
};
template<class F>
private ref class lambda_wrapper
{
public:
lambda_wrapper(const F& f)
{
pin_ptr<F> pf = (interior_ptr<F>)&f_storage;
new(pf) F(f);
}
~lambda_wrapper()
{
pin_ptr<F> pf = (interior_ptr<F>)&f_storage;
pf->~F();
}
template <class D>
operator D ^ ()
{
D^ d = nullptr;
return gcnew D(this, &lambda_wrapper<F>::invoke<decltype(return_type_helper::dummy<D>(0))>);
}
private:
template<class T>
[System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential, Size = sizeof(T))]
value struct embedded_storage
{
private:
typename aligner<T>::type dummy;
};
embedded_storage<F> f_storage;
template<class R, class A1>
R invoke(A1 a1)
{
pin_ptr<F> pf = (interior_ptr<F>)&f_storage;
return (*pf)(a1);
}
template<class R, class A1, class A2>
R invoke(A1 a1, A2 a2)
{
pin_ptr<F> pf = (interior_ptr<F>)&f_storage;
return (*pf)(a1, a2);
}
};
template <typename...>
ref class DelegateHelper;
template<class TParam1, class TResult>
ref class DelegateHelper<TParam1, TResult>
{
private:
Func<TParam1, TResult>^ _lambda;
TParam1 _param1;
TResult Execute()
{
return this->_lambda(this->_param1);
}
public:
template<class TLambda>
DelegateHelper(TLambda lambda, TParam1 param1)
{
this->_lambda = gcnew helper::lambda_wrapper<TLambda>(lambda);
this->_param1 = param1;
}
static operator Func<TResult> ^ (DelegateHelper<TParam1, TResult>^ value)
{
return gcnew Func<TResult>(value, &DelegateHelper<TParam1, TResult>::Execute);
}
};
template<class TParam1, class TParam2, class TResult>
ref class DelegateHelper<TParam1, TParam2, TResult>
{
private:
Func<TParam1, TParam2, TResult>^ _lambda;
TParam1 _param1;
TParam2 _param2;
TResult Execute()
{
return this->_lambda(this->_param1, this->_param2);
}
public:
template<class TLambda>
DelegateHelper(TLambda lambda, TParam1 param1, TParam2 param2)
{
this->_lambda = gcnew helper::lambda_wrapper<TLambda>(lambda);
this->_param1 = param1;
this->_param2 = param2;
}
static operator Func<TResult> ^ (DelegateHelper<TParam1, TParam2, TResult>^ value)
{
return gcnew Func<TResult>(value, &DelegateHelper<TParam1, TParam2, TResult>::Execute);
}
};
}
This is how to use it:
String^ parameter1 = L"value1";
String^ parameter2 = L"value2";
Main^ main = gcnew Main();
auto lambda1 = [](String^ parameter) -> String^
{
Threading::Thread::Sleep(1000);
return parameter;
};
main->ExpensiveMethodDelegate = gcnew helper::DelegateHelper<String^, String^>(lambda1, parameter1);
main->DoWork();
auto lambda2 = [](String^ parameter1, String^ parameter2) -> String^
{
Threading::Thread::Sleep(1000);
return parameter1 + parameter2;
};
main->ExpensiveMethodDelegate = gcnew helper::DelegateHelper<String^, String^, String^>(lambda2, parameter1, parameter2);
main->DoWork();
Not sure if it is the most elegant way, but it does the work I was looking for.

Can't compile thread example in Visual C++ 2005 using "oldSyntax" switch

I am trying to follow this example on how to create threads in a windows form. Purely following the example I get multiple syntax errors upon building.
I am using the /clr:oldSyntax compiler switch to compile the example.
Initialization of the Form1 class is the first source of the error:
public ref class Form1 : public System::Windows::Forms::Form
{
Errors:
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2143: syntax error : missing ';' before '{'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2447: '{' : missing function header (old-style formal list?)
Where exactly are these errors coming from?
Full Code:
#pragma once
using namespace System::Threading;
namespace ThreadExample
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::ProgressBar^ progressBar1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
private: Thread *trd;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(197, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(94, 162);
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(100, 23);
this->progressBar1->TabIndex = 1;
this->progressBar1->Click += gcnew System::EventHandler(this, &Form1::progressBar1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
ThreadStart *myThreadDelegate = new ThreadStart(this, repeat);
trd = new Thread(myThreadDelegate);
trd->IsBackground = true;
trd->Start();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
MessageBox::Show(S"This is the main thread");
}
private: System::Void progressBar1_Click(System::Object^ sender, System::EventArgs^ e)
{
}
};
__delegate void DelegateThreadTask();
private: void ThreadTask()
{
int stp;
int newval;
Random *rnd=new Random();
if (progressBar1->InvokeRequired == false)
{
stp=this->progressBar1->Step*rnd->Next(-1,2);
newval = this->progressBar1->Value + stp;
if (newval > this->progressBar1->Maximum)
newval = this->progressBar1->Maximum;
else if (newval < this->progressBar1->Minimum)
newval = this->progressBar1->Minimum;
this->progressBar1->Value = newval;
}
else
{
DelegateThreadTask *myThreadDelegate = new DelegateThreadTask(this,ThreadTask);
this->Invoke(myThreadDelegate);
}
}
private: void repeat()
{
while(true)
{
ThreadTask();
Thread::Sleep(100);
}
}
}
The old syntax for C++/CLI uses __gc to declare classes.
References:
C++/CLI language specification from Visual Studio 2003, when the 'old' syntax was the only syntax.
VS2005 documentation giving both old & new syntax for declaring managed types in C++/CLI.
Test 1:
public ref class Foo
{
};
Compile result:
OldSyntax.cpp
OldSyntax.cpp(1): error C2059: syntax error : 'public'
OldSyntax.cpp(2): error C2143: syntax error : missing ';' before '{'
OldSyntax.cpp(2): error C2447: '{' : missing function header (old-style formal list?)
Test 2:
public __gc class Foo
{
};
Compile result: no errors.
(Note: I used VS2012 for my test. I don't have VS2005 installed anymore; the error messages may be slightly different on 2005.)

Please fix my program error i m stuck

i am using visual C++ 2010 i m having just one error i want user to enter inputs model and color and then it will pass to a public variable and public member function will access private data members and assign values to it then a public member function will display those values main.when i try to write s1.colr[10] to pass it to fuunction it gives error.
include
using namespace std;
#include<conio.h>
class vehicle
{
private:
int d;
char color[10];
public:
int mdl;
char colr[10];
void get_input(int a,char b[10])
{
d=a ;
color[10]=b[10];
}
void disp()
{
cout<<"Model Number Is:"<<d ;
cout<<"Color Is:"<<color[10];
}
};
int main()
{
vehicle s1;
cout<<"Enter Model Number:";
cin>>s1.mdl;
cout<<"Enter Color:";
cin>>s1.colr[10];
s1.get_input(s1.mdl,s1.colr[10]);
s1.disp();
getch();
}
Use s1.colr insterad of so.colr[10]
cin>>s1.colr[10];
s1.get_input(s1.mdl,s1.colr[10]);
and you code will compile.
Don't use conio.h, #inlude <iostream>.
Live on coliru

Allowing a function to see listbox in another form

I have all my code in my .cpp file. I have a function in there:
void funct1 (void)
{
...
if (num_fields) {
for (ix = 0; ix < num_fields; ix++)
if (status == OK)
checkedListBox1->Items->Add(gcnew String(buffer));
} else
checkedListBox1->Items->Add("No available extra data fields");
}
However my function cannot see the checkedlistbox1 from my Form5.h.
How do I allow my function to see this?
I am calling my function from my cpp file:
System::Void Form5::MainMAFBrowseBtn_Click(System::Object^ sender,
System::EventArgs^ e) {
checkedListBox1->Items->Clear();
System::String^ paf_path2 = textBox1->Text;
FolderBrowserDialog^ folderBrowserDialog1;
folderBrowserDialog1 = gcnew System::Windows::Forms::FolderBrowserDialog;
folderBrowserDialog1->Description = L"Select the directory of your MAF files ";
folderBrowserDialog1->ShowNewFolderButton = false;
// Show the FolderBrowserDialog.
System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();
if ( result == ::DialogResult::OK )
paf_path2 = folderBrowserDialog1->SelectedPath;
textBox1->Text = paf_path2;
paf_path = (char*)(void*)Marshal::StringToHGlobalAnsi(paf_path2);
funct1();
}
If functl() is not part of the class, you'll need to pass in checkedListBox1 as a parameter, like so:
void funct1(System::Windows::Forms::CheckedListBox% checkedListBox1)
{
...
}
Than in your calling function:
System::Void Form5::MainMAFBrowseBtn_Click(System::Object^ sender, System::EventArgs^ e)
{
...
functl(checkedListBox1);
}
or inside your class Form5 you can put the declaration of functl there:
class Form5
{
...
private:
void functl();
};
Then in your .cpp file just declare functl as:
void Form5::functl()
{
/// Now you have direct access to checkedListBox1.
}

segfault happens when I serialize nested class in apache module

Serializing simple class "A" in Apache module done without error but when I tried to serialize my complex object like "X" which has a member, type of "A", I got segfault in Apache module. ( this doesn't happen to a executable console application )
------------------------- here is my code : ---------------------
class A {
private:
friend class boost::serialization::access; // to enable boost "access" class to call private "serialize" method of class "A"
template<class ArchT>
void serialize(ArchT &ar, unsigned int version) { // method for both serializing and deserializing
ar & memA; // (de)serialize member "memA"
}
std::string memA; // sample member
public:
A(){}
A(std::string pmemA) :
memA(pmemA) {
}
std::string GetMemA()
{
return memA;
}
};
class X {
private:
friend class boost::serialization::access;
template<class ArchT>
void serialize(ArchT &ar, unsigned int version) {
ar & memX;
ar & a;
}
std::string memX;
A a;
public:
X(std::string pmemX, A pa) :
memX(pmemX), a(pa) {
}
X(){}
};
-------------------
string st=GetRandomFileName();
ofstream out(st.c_str());
boost::archive::text_oarchive out_r(out);
A a("Amem");
X x("Xmem", a);
out_r << x; // not works
out_r << a; // works!
------------------- here is stack trace from gdb for apache ----------------
boost::serialization::typeid_system::extended_type_info_typeid_0::is_less_than(boost::serialization::extended_type_info const&) const () from /tmp/libIRSProWebApacheModule.so
2 0xb7223c61 in std::_Rb_tree