I have a bunch of code that looks like:

// Local block scope
{
	ostringstream  ostr;
	ostr.fixed;
	ostr.precision(2);
	ostr.width(20);
	ostr << foo->bar[trno];
	SafeArrayPut2D( retArray, 3, i, (void *) ostr.str().c_str() );
}
{
	ostringstream  ostr;
	ostr.fixed;
	ostr.precision(4);
	ostr.width(22);
	ostr << foo->foobar[trno];
	SafeArrayPut2D( retArray, 4, i, (void *) ostr.str().c_str() );
}

So I wanted to write a function to abbreviate the process. Here's what I did. In the header file:

void ostreamStringHelper( ostringstream& oss, streamsize prec, streamsize width );

In the cpp file:

void ostreamStringHelper( ostringstream& oss, streamsize prec, streamsize width )
{
	oss.fixed;
	oss.precision(prec);
	oss.width(width);
}

However, it's giving me errors:

1>Compiling...
1>SafeArrayUtil.cpp
1>c:\safearrayutil.cpp(32) : error C2027: use of undefined type 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Alloc=std::allocator<char>
1>        ]
1>c:\safearrayutil.cpp(32) : error C2228: left of '.fixed' must have class/struct/union
1>c:\projects\cashflow\trancheinfodll\safearrayutil.cpp(33) : error

I don't understand --- what exactly is the problem with the ostreamStringHelper() function? oss is a reference to a class, not a pointer; why is the syntax invalid?

Thanks!

I have a bunch of code that looks like:

// Local block scope
{
    ostringstream  ostr;
    ostr.fixed;
    ostr.precision(2);
    ostr.width(20);
    ostr << foo->bar[trno];
    SafeArrayPut2D( retArray, 3, i, (void *) ostr.str().c_str() );
}
{
    ostringstream  ostr;
    ostr.fixed;
    ostr.precision(4);
    ostr.width(22);
    ostr << foo->foobar[trno];
    SafeArrayPut2D( retArray, 4, i, (void *) ostr.str().c_str() );
}

So I wanted to write a function to abbreviate the process. Here's what I did. In the header file:

void ostreamStringHelper( ostringstream& oss, streamsize prec, streamsize width );

In the cpp file:

void ostreamStringHelper( ostringstream& oss, streamsize prec, streamsize width )
{
    oss.fixed;
    oss.precision(prec);
    oss.width(width);
}

However, it's giving me errors:

1>Compiling...
1>SafeArrayUtil.cpp
1>c:\safearrayutil.cpp(32) : error C2027: use of undefined type 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Alloc=std::allocator<char>
1>        ]
1>c:\safearrayutil.cpp(32) : error C2228: left of '.fixed' must have class/struct/union
1>c:\projects\cashflow\trancheinfodll\safearrayutil.cpp(33) : error 

I don't understand --- what exactly is the problem with the ostreamStringHelper() function? oss is a reference to a class, not a pointer; why is the syntax invalid?

Thanks!

It's hard to say, when you haven't given enough source code to determine what line the error is actually coming from. I implemented a quick demo:

void helper( ostringstream& oss, streamsize prec, streamsize width )
{
    oss.fixed;
    oss.precision( prec );
    oss.width( width );
}

int main(int argc, char *argv[])
{
    ostringstream ostr;
    helper( ostr, 3, 6 );
    return 0;
}

This compiled and ran fine for me using g++. Post the offending line or all of the offending file, and it will be easier to diagnose.

I wish I could use gcc... This is all of SafeArrayUtil.cpp:

#include "stdafx.h"
#include <atlconv.h> // T2OLE
#include <cassert>
#include "SafeArrayUtil.h"
using namespace std;



HRESULT SafeArrayPut2D( SAFEARRAY *sa, int row, int col, const void *data )
{
	USES_CONVERSION;
	long idx[2];
	HRESULT hResult;

	idx[0] = row;
	idx[1] = col;


	SafeArrayLock( sa );
	hResult = SafeArrayPutElement( sa, idx, SysAllocString(T2OLE((LPCSTR)data)) );
	assert( SUCCEEDED(hResult) );

	return hResult;
}


void ostreamStringHelper( ostringstream& oss, streamsize prec, streamsize w )
{
	oss.fixed;
	oss.precision( prec );
	oss.width( w );
}

And this is all of SafeArrayUtil.h:

#pragma once

#include <iostream>  // ostreamstring
#include <ostream>  // ostreamstring
#include <OLEAuto.h> // SAFEARRAY, BSTR definition

HRESULT SafeArrayPut2D( SAFEARRAY *sa, int row, int col, const void *data );
void ostreamStringHelper( std::ostringstream& oss, std::streamsize precision, std::streamsize width );

I don't call ostreamStringHelper() yet (since it doesn't compile), so this is the only translational unit the function appears in. Here is the resulting error message in its entirety:

1>------ Build started: Project: trancheInfoDLL, Configuration: Debug Win32 ------
1>Compiling...
1>trancheInfoDLL.cpp
1>SafeArrayUtil.cpp
1>c:\projects\safearrayutil.cpp(31) : error C2027: use of undefined type 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Alloc=std::allocator<char>
1>        ]
1>c:\projects\safearrayutil.cpp(31) : error C2228: left of '.fixed' must have class/struct/union
1>c:\projects\safearrayutil.cpp(32) : error C2027: use of undefined type 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Alloc=std::allocator<char>
1>        ]
1>c:\projects\safearrayutil.cpp(32) : error C2228: left of '.precision' must have class/struct/union
1>c:\projects\safearrayutil.cpp(33) : error C2027: use of undefined type 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Alloc=std::allocator<char>
1>        ]
1>c:\projects\safearrayutil.cpp(33) : error C2228: left of '.width' must have class/struct/union
1>Generating Code...
1>Creating browse information file...
1>Microsoft Browse Information Maintenance Utility Version 9.00.21022
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Build log was saved at "file://c:\Projects\Debug\BuildLog.htm"
1>trancheInfoDLL - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This is pretty much everything since the function isn't called anywhere. The compiler doesn't seem to recognize that the fact that oss is a reference to an ostreamstring.

Any ideas what could be wrong?

Thank you!!!
Pete

Try including <sstream> in SafeArrayUtil.h

Yep, I think you need to include sstream as well. stringstream is
defined in the sstream header file.

commented: Thank you for being patient with my dumb question! +1

Oh, for the love of Darwin.

Thank you for restoring my sanity (and sorry for the brain fart).

Oh, for the love of Darwin.

Thank you for restoring my sanity (and sorry for the brain fart).

Glad to help.

commented: Thank you for being patient with my stupid mistake! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.