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);
}
Related
In my C++/WinRT project I use below MIDL code to declare an asynchronous method returning a Windows::Foundation::IInspectable object.
namespace myproject
{
[default_interface]
runtimeclass FileRetriever
{
FileRetriever();
Windows.Foundation.IAsyncOperation<Windows.Foundation.IInspectable> RetrieveFileNamesAsync();
}
}
The code gives the following error when compiling:
Error MIDL2011 [msg]unresolved type declaration [context]: Windows.Foundation.IInspectable [ parameterized interface parameter 'Windows.Foundation.IInspectable' of Procedure 'RetrieveFileNamesAsync' ( RuntimeClass 'myproject.FileRetriever' ) ]
MSDN states the error can be resolved by "add[ing] an import directive for the IDL file(s) that contain the definitions of any type(s) that you reference that you've defined in your project" but IInspectable is obviously not a type I defined myself.
How can I resolve the error?
I would like to know if there's a way of declaring a ref class in a cpp file and then reference it in another ref class in another cpp file. both ref classes would be in the same namespace. This is what I'm trying to do:
//class1.cpp
namespace nm
{
public ref class class1
{
};
}
//class2.cpp
namespace nm
{
public ref class class2
{
private:
class1^ _member;
};
}
I get 2 strange errors from the compiler and one a bit clearer from Intellisense:
error C2143: syntax error : missing ';' before '^'
error C4430: missing type specifier - int assumed. Note: C++
does not support default-int
IntelliSense: identifier "class1" is undefined
If I use header files all this goes away. Is there a way to make it work without the need for header files? Do I need a smarter compiler?
I have following code
ref class A
{
typedef ref struct All
{
std::string x;
}All_t;
};
in my program I am using it in following manner
A::All_t t;
t.X = "H";
This declaration throwing error as
error C4368: cannot define 'x' as a member of managed 'A::All': mixed types are not supported
I understand that I am declaring native varible inside managed code which is not allowed but now I would like to know changes I will need to make to make my structure suitable to managed project.
Thanks.
I'm assuming you originally had std::string x; not std::string *x (since using the pointer to string does not generate that error). You are not allowed to directly embed a native type in a managed type, but you are allowed to indirectly have one (via a pointer) See:
http://msdn.microsoft.com/en-us/library/xhfb39es(v=vs.80).aspx
After I fixed the compiler errors in your sample, it builds without error:
#include "stdafx.h"
#include <string>
using namespace System;
ref class A
{
public:
typedef ref struct All
{
std::string * x;
}All_t;
};
int main(array<System::String ^> ^args)
{
A::All_t t;
t.x = new std::string("H");
return 0;
}
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?
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