I'm getting this error "Error C2059 syntax error: 'public'" in the first "public" of the HelloWorld.h file when I try to build the project. Looks like it's expecting something else but I'm a rookie on this. I have also tried using ref instead of __gc as the new syntax rules.
Does anybody have a clue of what can be missing here?
Thanks in advance.
HelloWorld.h
#using <mscorlib.dll>
#using "CSharpHelloWorld.netmodule"
using namespace System;
public __gc class HelloWorldC
{
public:
// Provide .NET interop and garbage collecting to the pointer.
CSharpHelloWorld __gc *t;
HelloWorldC() {
t = new CSharpHelloWorld();
// Assign the reference a new instance of the object
}
// This inline function is called from the C++ Code
void callCSharpHelloWorld() {
t->displayHelloWorld();
}
};
use ref class instead of __gc class in line 6
use ^ instead of __gc * in line 10
use gcnew instead of new in line 12 change
Common Language Runtime Support To (/clr) in your project Properties
Related
I'm trying to do something very simple... declare a string in a class, and then assign it to the value of another string defined in a class constructor.
I am using a managed wrapper for an unmanaged class "Unmanaged" (using a managed wrapper because I want to use it in a C# program, and something I am using is unmanaged and its .sln file is not under my control)
As you can see, I tried including as many string headers as possible.
#pragma once
#include <string>
#include <string.h>
#include <cstring>
#include <iostream>
using namespace std;
using namespace System;
using std::string;
namespace UnmanagedWrap {
public ref class Class1
{
// TODO: Add your methods for this class here.
public:
Unmanaged *pu; //pointer to the Unmanaged class
//the constructor will allocate the pointer pu
int a;
int b;
std::string filePath; //try CString() when get back
Class1(int a_In, int b_In, std::string filePath_In) : pu(new Unmanaged()) { //constructor
a = a_In;
b = b_In;
filePath = filePath_In; //trying to assign filePath to the inputted filePath_In.......
}; //end of constructor
This is giving me 2 errors:
The first relates to the line std::string filePath;
1>c:\users\ngrace\documents\visual studio 2010\projects\unmanagedwrap\unmanagedwrap\UnmanagedWrap.h(21): error C4368: cannot define 'filePath' as a member of managed 'UnmanagedWrap::Class1': mixed types are not supported
The second relates to the line filePath = filePath_In;
1>c:\users\ngrace\documents\visual studio 2010\projects\unmanagedwrap\unmanagedwrap\UnmanagedWrap.h(25): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
I am very lost as I have spent hours searching for an answer...
Some pages I have gone to for help:
Including headers from an unmanaged C++ code inside C++/CLI code
Mixed types are not supported
(I would post more but I need reputation of at least 10 to do so....)
Any ideas on why I'm getting these errors?
Using information from Bo Persson and user2666293, I was led to try something, which ended up being an answer to this.
You must use System::String^ type for managed strings. If using a managed string and passing it to a method in an unmanaged class, it must be converted to an unmanaged string type!
Let's say we're using the unmanaged string type std::string in the unmanaged class.
The conversion from System::String^ to std:string must be done using:
auto unmannedString = msclr::interop::marshal_as<std::string>(managedString);
and with a header file reference at the top of:
#include <msclr/marshal_cppstd.h>
where managedString is of type System::String^
:)
https://msdn.microsoft.com/en-us/library/hh699870.aspx says under bullet 2 that ref classes can only have standard c++ types if they are not public. Try using a regular class or making filePath private.
edit:
I'm not familiar with managed classes but you might try Platform::string instead of std::string : https://msdn.microsoft.com/en-us/library/hh755812.aspx
https://msdn.microsoft.com/en-us/library/hh699879.aspx
says to use platform strings when you pass strings back and forth to methods in Windows Runtime classes, or when you are interacting with other Windows Runtime components
I'm having issues with multithreading and multifile projects. Works fine when testing with a single file project but as I am trying to keep my headers separated from my implimentation, is there a way to make this work?
the error I am getting is:
error C3867: 'class1::Update': function call missing argument list; use '&class1::Update' to create a pointer to member
Sadly, the suggestion there doesn't work. Any help would be greatly appreciated.
Class1.H
class class1
{
public:
class1();
~class1();
private:
thread sThread;
void Update();
};
Class1.cpp
int class1::Initialize()
{
this->sThread = std::thread(Update);
}
As you say, the error is:
'class1::Update': function call missing argument list; use '&class1::Update' to create a pointer to member
So do that. Once you do you will find that you then need to use std::bind() to attach an instance of the class to the member function. That will look like:
thread(bind(&class1::Update, this))
I am writing a C++ WinRT Component DLL for use in my .NET-based WinRT application. The DLL defines a SoundSample ref class that creates an XAudio voice by calling IXAudio2::CreateSourceVoice. CreateSourceVoice takes a "IXAudio2VoiceCallback *pCallback" parameter to enable callbacks on various audio events. Now I am trying to implement that callback based on this article. XAudio will supposedly just call back into methods of my SoundCallback class defined as:
#pragma once
#include "xaudio2.h"
#include "pch.h"
class SoundCallback
: public IXAudio2VoiceCallback
{
private:
//SoundSample^ sample; //does not compile
public:
SoundCallback(void);
~SoundCallback(void);
//Called when the voice has just finished playing a contiguous audio stream.
void OnStreamEnd();
void OnVoiceProcessingPassEnd();
void OnVoiceProcessingPassStart(UINT32 SamplesRequired);
void OnBufferEnd(void * pBufferContext);
void OnBufferStart(void * pBufferContext);
void OnLoopEnd(void * pBufferContext);
void OnVoiceError(void * pBufferContext, HRESULT Error);
};
Everything is fine until I try to figure out how to call back from an instance of my native callback class to the parent SoundSample object. I was thinking I could pass an instance of the SoundSample class to the SoundCallback object, but it seems like it does not allow me to declare a ref class field in the native class:
SoundCallback.h(9): error C2143: syntax error : missing ';' before '^'
SoundCallback.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SoundCallback.h(9): error C3699: '^' : cannot use this indirection on type 'int'
I looked back at implementing callbacks in native C++ and I could not find a reasonable solution so far. What is the best/easiest way to do this?
Solved it (thanks to Jeremiah Morrill) - the problem is not with any barrier blocking the use of ref classes in basic classes. C4430 means that SoundSample is an unrecognized type, which was hidden by Intellisense - since that seemed to indicate that SoundSample is known.
What needs to be added is a declaration of the SoundSample type and this all starts working fine.
I just added
namespace MyNamespace { ref class SoundSample; }
before the SoundCallback class declaration and then SoundCallback class could declare:
MyNamespace::SoundSample^ sample;
I've got the following class definition:
class Portal
{
public:
Portal( const vector<vec3> &vertices, shared_ptr<Sector> target );
...
};
Somewhere else, I want to create an instanceof said class like this:
auto portal = make_shared<Portal>( portalVertices, target );
However, I get the following error message in Visual Studio 2010:
error C2668: 'boost::make_shared' : ambiguous call to overloaded
function
Can anyone tell me why? I only define a single constructor. Thank you!
As you are using the keyword auto I assume you are using C++11 features.
C++11 also comes with std::make_shared.
So, please try adding the namespace:
auto portal = std::make_shared<Portal>( portalVertices, target );
or
auto portal = boost::make_shared<Portal>( portalVertices, target );
So what I usually do in my code / the .C file is:
using namespace std; // this is a "using" directive
....
void somefunction() {
auto portal = make_shared<Portal>( ... );
}
As you mentioned you were specifying in your header
using boost::make_shared;
I would really like to see the full header file. As I think you actually wanted to have a using directive, but ended up in having a using declaration.
Have a look at this description:
using directive: http://msdn.microsoft.com/en-us/library/aewtdfs3%28v=vs.80%29.aspx
using declaration: http://msdn.microsoft.com/en-us/library/was37tzw%28v=vs.80%29.aspx
Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't seeing the rdbuf() procedure. Here is my code:
16. #include "DebugBuffer/BufferedStringBuf.h"
17.
18. BufferedStringBuf debug_buffer(256);
19. std::cout.rdbuf(&debug_buffer);
The BufferedStringBuf class is from this page: http://www.devmaster.net/forums/showthread.php?t=7037
Which produces the following error:
...src\main.cpp(19) : error C2143: syntax error : missing ';' before '.'
All I want to do is redirect std::cout to the Visual Studio Output window using OutputDebugString()..
You're not allowed to have executable statements at file-level scope. You can declare variables, but you can't call functions as standalone statements. Move your code into a function (such as gf's answer demonstrates), and you should have no problem.
Using the example class given on that site i don't have any problem:
#include <iostream>
#include "DebugBuffer/BufferedStringBuf.h"
class DbgBuf : public BufferedStringBuf {
public:
DbgBuf() : BufferedStringBuf(255) {}
virtual void writeString(const std::string &str) {}
};
int main()
{
DbgBuf debug_buffer;
std::cout.rdbuf(&debug_buffer);
}
Note that you have to create an instance of a class that derives from BufferedStringBuf because BufferedStringBuf::writeString() is pure virtual, thus making it an abstract class - abstract classes can't be instantiated.