ostream problems c++ - c++-cli

im not sure why i am having problems with ostream. If i use using namespace std; it throws up a bunch more errors like linker errors.
This is my code where i am having problems and the errors.
virtual void Put (ostream&) const;
error C2061: syntax error : identifier 'ostream'
error C2065: 'ostream' : undeclared identifier
error C2059: syntax error : ')'
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)
error C2061: syntax error : identifier 'ostream'
error C2061: syntax error : identifier 'ostream'
this is the container .h header file where i am having problems with
#ifndef CONTAINER_H
#define CONTAINER_H
#include <ostream>
#include <iostream>
#include "Object.h"
#include "NullObject.h"
#include "Ownership.h"
#include "Iterator.h"
#include "Visitor.h"
class Container : public virtual Object, public virtual Ownership
{
protected:
unsigned int count;
Container ();
public:
virtual unsigned int Count () const;
virtual bool IsEmpty () const;
virtual bool IsFull () const;
// virtual HashValue Hash () const;
virtual void Put (ostream&) const;
virtual Iterator& NewIterator () const;
virtual void Purge () = 0;
virtual void Accept (Visitor&) const = 0;
};
#endif
if i use
virtual void Put (std::ostream&) const;
it fixes the errors however, in the proceeding .cpp file i get the same errors above in the put function. i did try the std:: in the put functions but it threw up a ton of linker errors. i tried to use namespace std; also but that throws up a ton of linker errors.
#include "Container.h"
#include "NullIterator.h"
#include <ostream>
#include <iostream>
Container::Container () :
count (0)
{}
unsigned int Container::Count () const
{ return count; }
bool Container::IsEmpty () const
{ return Count () == 0; }
bool Container::IsFull () const
{ return false; }
Iterator& Container::NewIterator () const
{ return *new NullIterator (); }
void Container::Put(ostream&)const
{
return;
}
heres the errors i now get in that container.cpp file
error C2065: 'ostream' : undeclared identifier
error C2059: syntax error : ')'
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)
i tried to #include fstream
I will appreciate any help here. theres a ton more code but i dont think you need to see the other files.

Maybe you forget the std namespace. ostream is declared in std namespace, so you need to add "using namepsace std" before the declaration of Container or use scope resolution (std::ostream).

void Container::Put(ostream&)const
{
return;
}
I'm still a little new at this, but don't you need a variable name here?

Related

WinRT C++ component with non primitive function return types fails to compile/build

The below program works fine while having functions with primitive return type functions specifically the Pop function defined below works and I'm able to build properly, but when I build the solution with a function having non primitive return type as in PopC like below project doesn't build, but definitions are syntatically correct and I'm able to see the types being referred properly with proper namespace definitions in pch.h file.
Have defined the below code in the StreamSamplePool.idl file,
namespace WinRTComponent
{
[default_interface]
runtimeclass StreamSamplePool
{
StreamSamplePool();
Int32 MyProperty;
// MediaStreamSample PopC();
Int32 Pop();
}
}
And inside the StreamSamplePool.cpp file have defined the respective methods as follows
#include "pch.h"
#include "StreamSamplePool.h"
#include "StreamSamplePool.g.cpp"
#include <stack>
#include <queue>
using namespace winrt::Windows::Storage::Streams;
using namespace winrt::Windows::Foundation;
using namespace std;
//using namespace Platform;
using namespace winrt::Windows::Media::Core;
namespace winrt::WinRTComponent::implementation
{
int32_t StreamSamplePool::MyProperty()
{
throw hresult_not_implemented();
}
int32_t StreamSamplePool::Pop()
{
return int32_t();
}
//MediaStreamSample StreamSamplePool::PopC()
//{
// MediaStreamSample sample = MediaStreamSample::CreateFromBuffer(NULL,
//TimeSpan());
// //sample.Processed += OnSampleProcessed;
// return sample;
//}
void StreamSamplePool::MyProperty(int32_t /* value */)
{
throw hresult_not_implemented();
}
}
And have defined the following code inside StreamSamplePool.h
namespace winrt::WinRTComponent::implementation
{
struct StreamSamplePool : StreamSamplePoolT<StreamSamplePool>
{
StreamSamplePool() = default;
int32_t Pop();
//MediaStreamSample PopC();
int32_t MyProperty();
void MyProperty(int32_t value);
};
}
namespace winrt::WinRTComponent::factory_implementation
{
struct StreamSamplePool : StreamSamplePoolT<StreamSamplePool,
implementation::StreamSamplePool>
{
};
}
Error info
Error MIDL2011 [msg]unresolved type declaration [context]: WinRTComponent.MediaStreamSample [ Procedure 'PopC' ( RuntimeClass
'WinRTComponent.StreamSamplePool' ) ] WinRTComponent D:\Prithvi\Work\WinRTComponent\StreamSamplePool.idl 9
Compiler output
1>D:\...\StreamSamplePool.idl(9): error MIDL2011: [msg]unresolved type declaration [context]: WinRTComponent.MediaStreamSample [ Procedure 'PopC' ( RuntimeClass 'WinRTComponent.StreamSamplePool' ) ]
1>Done building project "WinRTComponent.vcxproj" -- FAILED.
The issue can be reduced to the following IDL file:
namespace WinRTComponent
{
[default_interface]
runtimeclass StreamSamplePool
{
StreamSamplePool();
MediaStreamSample PopC();
}
}
Compiling this using a default-generated C++/WinRT Component project produces the following error:
1>C:\...\StreamSamplePool.idl(7): error MIDL2011: [msg]unresolved type declaration [context]: WinRTComponent.MediaStreamSample [ Procedure 'PopC' ( RuntimeClass 'WinRTComponent.StreamSamplePool' ) ]
It contains all the information required to resolve the issue. In particular it's naming the error ("unresolved type declaration"), and the procedure affected ("PopC"). To solve this you'll have to provide the fully-qualified type name in the IDL file, i.e.
namespace WinRTComponent
{
[default_interface]
runtimeclass StreamSamplePool
{
StreamSamplePool();
Windows.Media.Core.MediaStreamSample PopC();
}
}
With that done, go to the "Generated Files\sources" directory under the project root, and copy the respective parts of the "StreamSamplePool.h" and "StreamSamplePool.cpp" files that the build system generated to your source files.

Unresolved external symbol while not actually using the function

Given the following C++/CLI program:
#pragma managed(push, off)
class FooBar
{
FooBar(const FooBar& that) { *this = that; }
const FooBar& operator =(const FooBar& that);
};
#pragma managed(pop)
int main()
{
return 0;
}
Gives the following linker output:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "private: class FooBar const & __thiscall FooBar::operator=(class FooBar const &)" (??4FooBar##AAEABV0#ABV0##Z) referenced in function "private: __thiscall FooBar::FooBar(class FooBar const &)" (??0FooBar##AAE#ABV0##Z) ConsoleApplication1 C:\Users\SomeUser\Documents\Visual Studio 2019\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.obj 1
But the following C++ program does not:
class FooBar
{
FooBar(const FooBar& that) { *this = that; }
const FooBar& operator =(const FooBar& that);
};
int main()
{
return 0;
}
The reason I need this to work is because I'm including a header file which is beyond my control that does this. Although I never need to use the copy constructor or the assignment operator.
Compiled using v142 platform toolset.

Convert C++/CLI User Defined Object (%) to Native C++ object (&)

How can I convert a user defined object passed by (%) to a native object (&). For example
void managed_func(user_defined_managed_obj% m_obj_)
{
// i need this
obj_ptr->unmanaged_func(&m_obj_);
}
Thus, in the above example I would like to pass the managed object %m_obj_ to the argument of the unmanaged_func. Here user_defined_managed_obj is a .net wrapper in C++/CLI that is a bridge for a native C++.
I've tried the following:
pin_ptr<user_defined_unmanaged_obj> tmp_ptr = &m_obj_;
obj_ptr->unmanaged_func(*tmp_ptr);
Will someone point me into the right direction or let me know what I can do to remedy this problem. Thank you.
Edit:
The user_defined_unmanaged_obj is a native C++ object and the user_defined_managed_obj is the wrapper C++/CLI class for it.
Edit:
The following are the code
In native C++, native_sql.h
#include <string>
class native_sql
{
public:
native_sql(string arguments)
{...some codes}
~native_sql(void) = default;
void connect(void) {...some codes}
void clean(void) {...some codes}
}
In native C++, native_upload_specific_data.h
#include "native_sql.h"
#include <string>
class native_upload_specific_data
{
public:
native_upload_specific_data(string arugments) {...some codes}
~native_upload_specific_data(void)
void upload(native_sql& sql_obj_) {...some codes}
}
In C++/CLI, sql_wrapper.h
#include <msclr\marshal_cppstd.h>
#include "../native_sql.h"
#include "../native_sql.cpp"
using namespace System;
public ref class sql_wrapper
{
public:
sql_wrapper(String^ arguments)
{...codes to convert String^ to std::string and pass args to
new native_sql}
~sql_wrapper(void) { delete sql_ptr; }
void connect_wrapper(void) { sql_ptr->connect(); }
void clean_wrapper(void) { sql_ptr->clean(); }
native_sql* sql_ptr;
}
In C++/CLI wrapper, upload_specific_data_wrapper.h
#include "../native_upload_specific_data.h"
#include "../native_upload_specific_data.cpp"
using namespace System;
public ref class upload_specific_data_wrapper
{
public:
upload_specific_data_wrapper(String^ arguments)
{...convert String^ args into std::strings and pass to
native_upload_specific_data ctor}
~upload_specific_data_wrapper(void) { delete data_ptr; }
void upload(sql_wrapper% sql_obj_)
{
// here is where I have the problem
pin_ptr<native_sql*> ptr = &(sql_obj_.sql_ptr);
data_ptr->upload(ptr);
}
native_upload_specific_data* data_ptr;
}
The error I receive is
C2664: 'void native_upload_specific_data(native_sql&)': cannot convert arugment 1 from cli::pin_ptr<native_sql*> to native_sql&
....Thank you.

Importing an Embarcadero C++ Builder XE3 DLL into Embarcadero C++ Builder XE3

I try to create a DLL in Embarcadero C++ Builder XE3, and use it in a test-project in the same environment.
I take example on a tutorial which code does not give a good result for me (!) : http://docwiki.embarcadero.com/RADStudio/XE3/en/Tutorial:_Using_Dynamic_Linked_Libraries_in_C%2B%2BBuilder_Applications
Here is the content of my DLL :
BaseAuth.h file :
#ifndef BaseAuthH
#define BaseAuthH
#include <System.hpp>
class TBaseAuth
{
public:
virtual void TestMessage() = 0;
};
#endif // BaseAuthH
Auth.h file :
//---------------------------------------------------------------------------
#ifndef AuthH
#define AuthH
//---------------------------------------------------------------------------
#include "BaseAuth.h"
class TAuth : public TBaseAuth
{
public:
TAuth();
~TAuth();
void TestMessage();
};
#endif
Auth.cpp file :
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Auth.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
TAuth::TAuth()
{
}
TAuth::~TAuth()
{
}
void TAuth::TestMessage()
{
MessageBox(0, "Test message", "Test", MB_OK);
}
and File1.cpp :
#pragma hdrstop
#pragma argsused
#include "Auth.h"
extern "C" __declspec(dllexport) void* __stdcall GetClassInstance()
{
return static_cast<void*>(new TAuth());
}
extern "C" int _libmain(unsigned long reason)
{
return 1;
}
Now in the test application I have :
the same BaseAuth.h file
a form with a Button :
Test_DLLAuthOrga.h :
#ifndef Test_DLLAuthOrgaH
#define Test_DLLAuthOrgaH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
#include "BaseAuth.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // Composants gérés par l'EDI
TButton *Button2;
void __fastcall Button2Click(TObject *Sender);
private: // Déclarations utilisateur
TBaseAuth *mpAuth;
public: // Déclarations utilisateur
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Test_DLLAuthOrga.cpp :
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Test_DLLAuthOrga.h"
#include "BaseAuth.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
const wchar_t* library = L"DLLAuthOrga.dll";
extern "C" __declspec(dllimport) void* __stdcall GetClassInstance();
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
HINSTANCE load;
try
{ load = LoadLibrary(library); }
catch(Exception &e)
{ ShowMessage(e.Message); }
if (load)
{
ShowMessage("Library Loaded!");
void *myFunc;
myFunc = (void *)GetProcAddress(load, "GetClassInstance");
mpAuth = (AuthParent*)myFunc;
}
else { ShowMessage("Library not loaded!"); }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (mpAuth == NULL) return;
try { pRes = mpAuth->TestMessage(); }
catch(Exception &e) { ShowMessage(e.Message); return; }
}
The result is :
the pointer mpAuth has an adress.
But its methods have no adress, and when I call a simple method such as "void TestMessage()", it raises an access violation.
=> It first seemed to be a question of string compatibility (but between "C++ Builder XE3" and "C++ Builder XE3" I would expect the same string format to be used ?!) : Error calling DLL with Unicode Delphi
=> I found a similar issue but with C++ DLL into Delphi, not C++ DLL into C++ ... : Call C++ DLL in Delphi app
=> I tried using "HMODULE" instead of "HINSTANCE load;" : same result.
=> I tried without success using
mpAuth = static_cast<AuthParent*>(myFunc);
instead of :
mpAuth = (AuthParent*)myFunc;
=> I also tried replacing "__stdcall" by "__cdecl" or "" (removing) : the libray loads but GetProcAdress returns NULL.
=> What am I doing wrong in attempting to call the DLL's method "TestMessage()" ?
To correctly bind function from dll you should give it full definition, including calling convention, arguments, return type and __dllexport/__dllimport modifier. The easiest way to do it - using typedef so instead of typing (in Test_DLLAuthOrga.cpp)
void *myFunc;
myFunc = (void *)GetProcAddress(load, "GetClassInstance");
use
typedef __declspec(dllimport) void (__stdcall *MyFuncPointerType)(void);
MyFuncPointerType myFunc;
myFunc = (MyFuncPointerType)GetProcAddress(load, "GetClassInstance");
If you are using __cdecl convention you should also add underscore to the target function name
typedef __declspec(dllimport) void (__cdecl *MyFuncPointerType)(void);
MyFuncPointerType myFunc;
myFunc = (MyFuncPointerType)GetProcAddress(load, "_GetClassInstance");
you can also explicitly define AuthParent* as a return type for your factory function to get rid of uneccessary casts to void* and back to AuthParent* (in File1.cpp and Test_DLLAuthOrga.cpp) so, the final code snippet would look like this:
typedef __declspec(dllimport) AuthParent* (__cdecl *MyFuncPointerType)(void);
MyFuncPointerType myFunc;
myFunc = (MyFuncPointerType)GetProcAddress(load, "_GetClassInstance");
mpAuth = myFunc();

SFML Input system problem

So I was porting my game engine from SDL to SFML, and now I have a problem with my input system.
Input.h
#ifndef BULLWHIP_INPUT_H
#define BULLWHIP_INPUT_H
#include
class bc_Input
{
public:
bool bm_KeyHit(sf::Key::Code key);
bool bm_KeyDown(sf::Key::Code key);
int bm_MouseX();
int bm_MouseY();
void bm_init(sf::RenderWindow app);
private:
sf::RenderWindow App;
const sf::Input& input;
};
#endif
Input.cpp
#include "Input.h"
bool bc_Input::bm_KeyDown(sf::Key::Code key)
{
return in.IsKeyDown(key)
}
bool bc_Input::bm_KeyHit(sf::Key::Code key)
{
sf::Event event;
while(App.GetEvent(event) && event.Type == sf::Event::KeyPressed)
{
switch(event.Key.Code)
{
case key: return true; break;
default:
break;
}
}
}
void bc_Input::bm_init(sf::RenderWindow app)
{
App = app;
in = App.GetInput();
}
int bc_Input::bm_MouseX()
{
return in.GetMouseX();
}
int bc_Input::bm_MouseY()
{
return in.GetMouseY();
}
I get these errors from this:
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: 'bc_Input::App' cannot appear in a constant-expression
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: '.' cannot appear in a constant-expression
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: a function call cannot appear in a constant-expression
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: ISO C++ forbids initialization of member 'input'
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: making 'input' static
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: invalid in-class initialization of static data member of non-integral type 'sf::Input&'
c:\program files (x86)\codeblocks\mingw\bin../lib/gcc/mingw32/4.4.0/../../../../include/SFML/System/NonCopyable.hpp:57: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private
c:\program files (x86)\codeblocks\mingw\bin../lib/gcc/mingw32/4.4.0/../../../../include/SFML/Window/Window.hpp:56: error: within this context
You're calling the copy constructor here:
void bc_Input::bm_init(sf::RenderWindow app)
{
App = app;
in = App.GetInput();
}
Note one of your error messages:
error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)
In order to avoid this problem, you should use an actual constructor for your sf::Input object along with an initialization list.
Alternatively, if you've already initialized a sf::RenderWindow in a higher subsystem (as is likely the case), simply changing your member variable App to a reference should do the trick.
//header
sf::RenderWindow& App;
...
//source
void bc_Input::bm_init(sf::RenderWindow& app)
{
App = app;
in = App.GetInput();
}