calling std::cout.rdbuf() produces syntax error - c++

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.

Related

Child form calling function on parent in .NET c++

I'm having an application with 2 winforms: Form1.h and TrackEdit.h. They're both in the same namespace ("ParkCleanUp2").
From within Form1 I call this code:
ParkCleanUp2::TrackEdit^ te;
Where it gives me these errors:
Error 24 error C2039: 'TrackEdit' : is not a member of 'ParkCleanUp2' (TrackEdit.cpp) c:\users\-joey\documents\visual studio 2010\projects\park cleanup 2\park cleanup 2\Form1.h 2332
Error 25 error C2065: 'TrackEdit' : undeclared identifier (TrackEdit.cpp) c:\users\-joey\documents\visual studio 2010\projects\park cleanup 2\park cleanup 2\Form1.h 2332
Error 26 error C2065: 'te' : undeclared identifier (TrackEdit.cpp) c:\users\-joey\documents\visual studio 2010\projects\park cleanup 2\park cleanup 2\Form1.h 2332
Though, if I go to TrackEdit.h it show me:
namespace ParkCleanUp2 {
//Some namespae includes
public ref class TrackEdit : public System::Windows::Forms::Form
So I'm wondering why it's giving me the error "'TrackEdit' : is not a member of 'ParkCleanUp2'" and why it's looking into the TrackEdit.cpp file, while I included the .h file.
What I found to be weird, and maybe important to mention, is that when I comment the #include "Form1.h line in TrackEdit.h it just works perfect, but in TrackEdit.h I than can't call Form1's functions (like selected an item in a listbox) which I wanted to achieve.
It appears you have both Form1.h and TrackEdit.h each #include-ing the other. Instead, have a forward declaration, and only include Form1.h from TrackEdit.cpp, and vise-versa.
The double-include doesn't work because you've got both classes referencing the other. Each class needs to know about the other in order to define itself. Since all you have is the full class definition, you've got a circular definition. Instead, the forward declaration provides just enough for the compiler to know "OK, there's a class with that name, and that's all I know about it", and the circular dependency is resolved.
(Also: When you edited the question, you removed the most important sentence: "so basically Form1.h includes TrackEdit.h, which includes Form1.h again". That pattern is very rarely correct. If you see yourself doing that, provide more forward declarations instead.)
Something like this:
Form1.h:
namespace ParkCleanUp2 {
ref class TrackEdit;
public ref class Form1 {
TrackEdit^ track;
};
}
TrackEdit.h:
namespace ParkCleanUp2 {
ref class Form1;
public ref class TrackEdit {
Form1^ parentForm;
};
}
Form1.cpp and TrackEdit.cpp:
#include "Form1.h"
#include "TrackEdit.h"

C++ class declaration error in XCode

I am porting a C++ project to Mac using Xcode. Now I have a class that is declared as following:
namespace poi {
class AesCipher : public SymetricCipher {
public:
AesCipher();
virtual ~AesCipher ();
//more method declarations here...
};
}
Yet Xcode will present me an error for the line of the public: statement:
C++ requires a type specifier for all declarations
I assume this has to do with the namespace but I'm not that experienced with C++.
I have no idea what this error is trying to say to me. Maybe someone has seen such an error before and could just point me in the right direction what kind of mistake I might have made.

What is the best way to get a callback from an instance of a native C++ class to a C++/CX ref class?

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;

unexplained syntax error defining threadpool inside class definition

I have the following class definition and for some reason I cannot define the threadpool inside the class definition itself. It says: syntax error: identifier 'numberofpoolthreads' I tried directly defining it in the class, but it gives me same syntax error, does anyone know why this is?
#include "stdafx.h"
#include <boost/threadpool.hpp>
#include <boost/threadpool/pool.hpp>
#include <boost/threadpool/detail/pool_core.hpp>
typedef boost::threadpool::fifo_pool resolverpool;
class ResolverThreadPoolManager
{
public:
ResolverThreadPoolManager::ResolverThreadPoolManager(int numberofpoolthreads);
ResolverThreadPoolManager::~ResolverThreadPoolManager();
resolverpool p(numberofpoolthreads);
private:
int numberofpoolthreads;
};
This line: resolverpool p(numberofpoolthreads); isn't valid in a class definition. You need a member variable, which you then initialise in the constructor. e.g.:
class ResolverThreadPoolManager
{
public:
explicit ResolverThreadPoolManager(int numberofpoolthreads);
...
private:
const resolverpool p;
};
ResolverThreadPoolManager::ResolverThreadPoolManager(int numberofpoolthreads)
: p(numberofpoolthreads)
{}
In your line
resolverpool p(numberofpoolthreads);
the argument "numberofpoolthreads" is not a type, so this is a malformed declaration. Perhaps
resolverpool p(int numberofpoolthreads);
? I imagine, perhaps incorrectly, that your error message also hints on which line the error occurred, which can help narrow down where in a file an error lies. (Although, typically only indicating "error is on this or some previous line.")

Expected class name before ',' or ';'

I am getting an "Expected class-name before , or ; and I dont quite get how to fix it.
Here is where the error is:
class FXHost : public CPLAT::CP_Application, public CPLAT::CP_M_Listener
{
The file is FXHost.h and CPLAT:: is obviously a namespace where CP_Application and CP_M_Listener are.
I dont see why this would be wrong. This code ran fine in Metrowerks (without the CPLAT::) but in XCode CPLAT is needed due to the way the code was written by the previous developer.
Can anyone shed some light on this error?
UPDATE: Here is a sample of the CP_Application class
template <class DOC_POLICY, class PRINT_POLICY, class UNDO_POLICY>
class CP_EXPORT CP_Application : public CP_Application_Imp
{
public:
typedef DOC_POLICY DocPolicyType;
typedef PRINT_POLICY PrintPolicyType;
typedef UNDO_POLICY UndoPolicyType;
CP_Application();
virtual ~CP_Application() throw();
It looks like the compiler hasn't seen the class declaration for the two parent classes. The first thing I would check are your include directives. Are you sure you're including header which defines the classes CP_Application and CP_M_Listener?