0

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!

Edited by caffeine: n/a

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by dusktreader
0

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.

Edited by mike_2000_17: Fixed formatting

0

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

Edited by caffeine: n/a

1

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

Votes + Comments
Thank you for being patient with my dumb question!
0

Oh, for the love of Darwin.

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

1

Oh, for the love of Darwin.

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

Glad to help.

Votes + Comments
Thank you for being patient with my stupid mistake!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.