Error C2447, C2143, C2059: Basic Visual C++ Class Definition - c++-cli

I'm just trying to create a class in visual C++ but keep getting the three errors mentioned above:
Line 9: error C2447: '{' : missing function header (old-style formal list?) in UIAutomationCPP.cpp
Line 9: error C2143: syntax error : missing ';' before '{' in UIAutomationCPP.cpp
Line 9: error C2059: syntax error : ')' in UIAutomationCPP.cpp
My code for the project is as follows:
// File Name: AutomationCPP.h
#pragma once
#ifndef AUTOMATIONCPP_H
#define AUTOMATIONCPP_H
#include "Stdafx.h"
using namespace System;
namespace AutomationCPP
{
public ref class CustomAutomationCPP
{
public:
CustomAutomationCPP();
int first;
private:
int second;
};
}
#endif
And the class .cpp file:
// File Name: AutomationCPP.cpp
#include "Stdafx.h"
#include "AutomationCPP.h"
using namespace System;
AutomationCPP::CustomAutomationCPP()
{
}
Please help! I feel if I can get past this, the rest should be much easier.

A constructor is a special member function:
AutomationCPP::CustomAutomationCPP::CustomAutomationCPP()
{
}

Alternatively to Etienne's, which I find personally more readful as it removes some noise from the members:
namespace AutomationCPP {
CustomAutomationCPP::CustomAutomationCPP()
{
}
void CustomAutomationCPP::foo()
{
}
// ...
} // namespace AutomationCPP

Related

why i can't wrap methods from one dll but can do it for another?

Im trying to write a wrapper in c++/cli for an DLL, which code i dont have, only DLL file and header but i created lib file through VS command prompt. When i`m trying to build solution i receive this errors:
DotNetWrappOfAsterSdkDll.obj : error LNK2028: unresolved token (0A00002E) "void __stdcall MuteClearLastError(void)" (?MuteClearLastError##$$FYGXXZ) referenced in function "public: void __clrcall DotNetWrappOfAsterSdkDll::WrapperClass2::doMuteClearLastError(void)" (?doMuteClearLastError#WrapperClass2#DotNetWrappOfAsterSdkDll##$$FQ$AAMXXZ)
DotNetWrappOfAsterSdkDll.obj : error LNK2019: unresolved external symbol "void __stdcall MuteClearLastError(void)" (?MuteClearLastError##$$FYGXXZ) referenced in function "public: void __clrcall DotNetWrappOfAsterSdkDll::WrapperClass2::doMuteClearLastError(void)" (?doMuteClearLastError#WrapperClass2#DotNetWrappOfAsterSdkDll##$$FQ$AAMXXZ)
I tried to create my own DLL and include it to the wrapper, and its working perfectly
here dll created by me which i can use in c++/cli wrapper:
//header file
#pragma once
#define DLLEXP __declspec( dllexport )
namespace Computations {
DLLEXP void someMethod(int number);
}
//cpp file
#include "Computations.h"
#include <iostream>
#include <time.h>
//#include "pnl/pnl_random.h"
using namespace std;
void Computations::someMethod(int number)
{
std::cout << "something "<<number*number << endl;
}
and here is part of header of DLL which i want to use:
#ifndef MUTEIFC_H
#define MUTEIFC_H
#include <Windows.h>
#ifdef MUTEIFC_LIBRARY
# define MUTEAPI extern "C"__declspec(dllexport)
#else
# define MUTEAPI __declspec(dllimport)
#endif
#define MUTECALL __stdcall
/** \ingroup init */
/** Initialization of the ASTER SDK library
* \returns TRUE - success, FALSE - failure (use \ref MuteLastErrorCode or/and \ref MuteLastErrorInfo to get
* failure cause)
* \note This function will require Administrative privileges on the first call on a given computer.
*/
MUTEAPI BOOL MUTECALL MuteIfcInitialize(VOID);
/** \ingroup init */
/** Finialization of the ASTER SDK library
*/
MUTEAPI VOID MUTECALL MuteIfcFinalize(VOID);
/** \ingroup errors*/
/** Clears the calling thread's last-error code and description.
* The last-error is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error.
*/
MUTEAPI VOID MUTECALL MuteClearLastError(VOID);
#endif // MUTEIFC_H
and my c++/cli code :
//header file
#pragma once
#include "Computations.h"
#include "muteifc.h"
using namespace System;
namespace DotNetWrappOfAsterSdkDll
{
public ref class WrapperClass2
{
public:
void doMuteClearLastError();
};
public ref class WrapperClass
{
private:
public:
void getPriceCallEuro(int number);
};
}
//cpp file
#include "DotNetWrappOfAsterSdkDll.h"
using namespace DotNetWrappOfAsterSdkDll;
using namespace Computations;
namespace DotNetWrappOfAsterSdkDll
{
//this dont work
void WrapperClass2::doMuteClearLastError() {
MuteClearLastError();
}
//this works great
void WrapperClass::getPriceCallEuro(int number) {
someMethod(number);
//MuteIfcFinalize();
}
}
Please tell me what i'm doing wrong
You probably didn't add the lib that contains the function reference to the linker options.
Either the lib contains he code it self or it has a reference to the DLL that must be loaded. The linker will bring your code and the DLL (or static lib) code together...

Using vector of pointer to objects of another class

I have been trying to do the following to no avail,
In 'used.h',
#ifndef USED_H_
#define USED_H_
#include<iostream>
#include<string>
class used
{
public:
int member=0;
used();
virtual ~used();
};
#endif
In the used.cc,
#include "used.h"
used::used()
{
}
used::~used()
{
}
In 'the_user.h',
#ifndef THE_USER_H_
#define THE_USER_H_
#include<queue>
#include<iostream>
class used; //Class forward declaring
class the_user
{
public:
std::deque<used*> my_queue;
the_user();
~the_user();
};
#endif
Now, I want to access and change 'member' in 'the_user.cc',
#include "used.h"
#include "the_used.h"
#include<iostream>
#include<queue>
using namespace std;
the_user::the_user()
{
deque <used*> my_queue;
my_queue.resize(6);
used* object = new used; <-------marked line
for(unsigned int i=0; i<my_queue.size(); i++)
{
my_queue.push_back(object);
}
cout << my_queue[5] << endl; //Should give 0
my_queue[0]->member=1000;
cout << my_queue[0]->member << endl; //1000
}
in main file(I have only read access),
#include "the_used.h"
#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;
int main()
{
the_used *object = new the_used();
}
Actually, I am getting undefined reference to used::used() at the marked line. What seems to be the problem?
I have tried to use the same for a vector as well but to no avail.
I am not allowed to make changes to the 'int main(){}'.
Any help would be highly appreciated.
Your class declaration doesn't declare any constructor or destructor:
class used
{
public:
int member=0;
};
But in your cpp file you define them. Your compiler should complain already here:
#include "used.h"
used::used()
{
}
used::~used()
{
}
You must declare constructor and destructor in your class:
class used
{
public:
used();
~used();
int member=0;
};
Then here:
my_queue.resize(6);
you will actually create 6 pointers that will be initialized to nullptr. Maybe you're aware of that, since you expect my_queue[5] to return 0.
Then in your loop, everytime you do this:
my_queue.push_back(object);
you will increase the size of my_queue by one, thus make your loop run forever.
Apart from that: Do. Not. Do. using namespace std;. Ever.

C++/CLI how to pass integer pointer to C++ API

Looks like I am doing a basic mistake here. I have a 3 party C++ library(test.dll) in which there is a API defined as follows. And I am invoking this APi by loading the library, getting the API and invoke. I am new to C++ CLI, any pointers to solve the issue will be helpful. Thanks in advance.
3rd part library exported API
FUNCTION_EXPORT void STDCALL GetVersion(UINT16& version);
typedef void (STDCALL *GETVERSION)(UINT16);
I need to call it from C++ Cli
Header file
MyTest.h
namespace MyTest {
public ref class TestClass
{
public:
HMODULE module;
String^ version;
void TestMethod()
};
}
Cpp file
MyTest.cpp
namespace MyTest {
TestClass::TestMethod()
{
this->module = LoadLibrary(engineDllPath);
if (!this->module)
{
return String::Format("LoadLibrary failed");
};
// Get engine version
GETVERSION GetVersionApi = (GETVERSION)GetProcAddress(module, "GetVersion");
if (!GetVersionApi)
{
return;
}
UINT16 major;
GetVersionApi(&uiMajor);
}
}
Getting compilation error
error C2664: 'void (UINT16 &)' : cannot convert parameter 1 from 'UINT16 *' to 'UINT16 &'
Code snippet is to give an idea what I am trying. The main issue is here
UINT16 major;
GetVersionApi(&uiMajor);
what will be the correct way of calling it. Please help.
GetVersion(UINT16& version);
That's not an integer pointer, that's an integer reference. You don't need to type any extra characters to pass a reference.
GetVersionApi(uiMajor);
// ^ no "&"

Syntax error : missing ';' before '^' in c++ managed

In my_project.h :
#pragma once
#include <vcclr.h>
#include "MyManagedClass.h"
namespace my_namespace
{
MyManagedClass^ DoSomething(const Foo* foo);
}
I have got next errors:
1) error C2143: syntax error : missing ';' before '^'
2) error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
Project has been created as c++/cli.
in "MyManagedClass.h" :
public ref class MyManagedClass
{
public:
System::UInt32 GetMember()
{
return m_member;
}
private:
System::UInt32 m_member;
};
In other files everything works but here, what I did wrong ?
ETA:
I've modified the function like this:
namespace my_namespace
{
MyManagedClass^ DoSomething(const System::String^ str);
}
And it didn't solve the problem, but for some reason if I change return type to void, then everything works fine
I re-created your issue, and got the same error. Removing the parameter const Foo* foo (making method DoSomething take no parameters) removed the error. I also tried adding typedef char* Foo;, and the error went away as well.
Check your definition of Foo, it looks like that's where the error is.
I copied & pasted your code into Visual Studio, there was no error on the MyManagedClass type. You'll need to show us more code if we're going to find the error.
Perhaps is MyManagedClass in a namespace that you forgot the using namespace directive for?

Calling OpenXML objects in c++ project

How to call DocumentFormat and other relevant objects in C++/CLi with OpenXML as added reference into the program. I couldnot find any documentation on how to call these objects. "Using DocumentFormat.OpenXML.Package" gives error in c++ project such as follows:
"
1>clitest.cpp(7): error C2143: syntax error : missing ';' before '.'
1>clitest.cpp(7): error C2873: 'DocumentFormat' : symbol cannot be used in a using-declaration
1>clitest.cpp(7): error C2059: syntax error : '.'
"
However I have added DocumentFormat, as reference to my project.
Regards
YogiK
C++ namespace syntax uses :: as a delimiter. This code compiles::
#include "stdafx.h"
using namespace System;
using namespace DocumentFormat::OpenXml::Packaging;
int main(array<System::String ^> ^args)
{
WordprocessingDocument^ doc = WordprocessingDocument::Create("C:\\mydoc.xdoc",DocumentFormat::OpenXml::WordprocessingDocumentType::Document);
}