i have the following problem::

i have ofstream object (i call it fout) belonging to a class{it a field of that class}, where should i initialize it...

i write the following test-code but it gives me strange compile errors{in visual studio05}


FoutTest.h

#pragma once

#include <fstream>
#include <string>

class CFoutTest
{
public:    CFoutTest(string filename="test.dat");
public: ~CFoutTest(void);

public:
    ofstream fout;
    void print(string text);
};

FoutTest.cpp

#include "FoutTest.h"

CFoutTest::CFoutTest(string filename)
:fout(filename.c_str())
{
    //fout(filename.c_str(), ios_base::app| ios_base::out | ios_base:text)
}

CFoutTest::~CFoutTest(void)
{
}

CFoutTest::print(string text)
{
    fout<<text;
}

main.cpp

#include "FoutTest.h"

int main()
{
    CFoutTest test;

    test.print("dokimi");
}

plz help me find the error!!

thanks in advance,
N.A.

void CFoutTest::print(string text)
{
    fout<<text;
}

Everything compiled ok for me except above in red.

void CFoutTest::print(string text)
{
    fout<<text;
}

Everything compiled ok for me except above in red.

i made this correction and recompiled the project and i had 36 errors...:sad:

this is the output that i get from visual studio....

------ Rebuild All started: Project: fout_test, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'fout_test', configuration 'Debug|Win32'
Compiling...
FoutTest.cpp
\fouttest.h(8) : error C2146: syntax error : missing ')' before identifier 'filename'
\fouttest.h(8) : error C2146: syntax error : missing ';' before identifier 'filename'
\fouttest.h(8) : error C2460: 'CFoutTest::string' : uses 'CFoutTest', which is being defined
\fouttest.h(7) : see declaration of 'CFoutTest'
\fouttest.h(8) : error C2059: syntax error : ')'
\fouttest.h(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\fouttest.h(8) : error C2864: 'CFoutTest::filename' : only static const integral data members can be initialized within a class
\fouttest.h(12) : error C2146: syntax error : missing ';' before identifier 'fout'
\fouttest.h(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\fouttest.h(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\fouttest.h(13) : error C2327: 'CFoutTest::string' : is not a type name, static, or enumerator
\fouttest.h(13) : error C2061: syntax error : identifier 'string'
\fouttest.cpp(3) : error C2597: illegal reference to non-static member 'CFoutTest::string'
\fouttest.cpp(3) : error C2146: syntax error : missing ')' before identifier 'filename'
\fouttest.cpp(3) : error C2761: '{ctor}' : member function redeclaration not allowed
\fouttest.cpp(3) : error C2059: syntax error : ')'
\fouttest.cpp(4) : error C2065: 'filename' : undeclared identifier
\fouttest.cpp(4) : error C2228: left of '.c_str' must have class/struct/union
        type is ''unknown-type''
\fouttest.cpp(5) : error C2448: 'fout' : function-style initializer appears to be a function definition
\fouttest.cpp(13) : error C2597: illegal reference to non-static member 'CFoutTest::string'
\fouttest.cpp(13) : error C2146: syntax error : missing ')' before identifier 'text'
\fouttest.cpp(13) : error C2761: 'void CFoutTest::print(void)' : member function redeclaration not allowed
\fouttest.cpp(13) : error C2059: syntax error : ')'
\fouttest.cpp(14) : error C2143: syntax error : missing ';' before '{'
\fouttest.cpp(14) : error C2447: '{' : missing function header (old-style formal list?)
main.cpp
\fouttest.h(8) : error C2146: syntax error : missing ')' before identifier 'filename'
\fouttest.h(8) : error C2146: syntax error : missing ';' before identifier 'filename'
\fouttest.h(8) : error C2460: 'CFoutTest::string' : uses 'CFoutTest', which is being defined
\fouttest.h(7) : see declaration of 'CFoutTest'
\fouttest.h(8) : error C2059: syntax error : ')'
\fouttest.h(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\fouttest.h(8) : error C2864: 'CFoutTest::filename' : only static const integral data members can be initialized within a class
\fouttest.h(12) : error C2146: syntax error : missing ';' before identifier 'fout'
\fouttest.h(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\fouttest.h(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\fouttest.h(13) : error C2327: 'CFoutTest::string' : is not a type name, static, or enumerator
\fouttest.h(13) : error C2061: syntax error : identifier 'string'
\main.cpp(7) : error C2660: 'CFoutTest::print' : function does not take 1 arguments
Generating Code...
Build log was saved at "file://c:\Documents and Settings\aggelidis\Τα έγγραφά μου\Visual Studio 2005\Projects\fout_test\fout_test\Debug\BuildLog.htm"
fout_test - 36 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

any ideas?


PS::the problem seems to be with the ofstream declaration{ofstream fout}......but i can't figure out why....

You could try changing this:
public: CFoutTest(string filename="test.dat");

to this:
public:
CFoutTest();
CFoutTest(string);

in the declarations and then in the definitions use this:

CFoutTest() : fout("test.dat") {}
CFoutTest(string filename) : fout(filename.c_str()) {}

your problem is that the program is not aware of the std namespace. You have two choices:
1. after including the header files add this using namespace std; or add std:: in front of each object declaration. std::string filename

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.