Code causes errors within include files???

following code for some reason, causes following errors withing its include files, when compiled with microsoft visual c++:
1>c:\program files\microsoft visual studio 9.0\vc\include\istream(846) : error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ios(152) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator =(const std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>c:\program files\microsoft visual studio 9.0\vc\include\fstream(548) : error C2248: 'std::basic_streambuf<_Elem,_Traits>::operator =' : cannot access private member declared in class 'std::basic_streambuf<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\streambuf(22) : see declaration of 'std::basic_streambuf<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_filebuf<_Elem,_Traits> &std::basic_filebuf<_Elem,_Traits>::operator =(const std::basic_filebuf<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>c:\program files\microsoft visual studio 9.0\vc\include\ostream(584) : error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ios(152) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;

const int MAX_CONT = 400;

class file{			
public:
	file(const char fileName[]);
	file();
	char *getcontents();
	bool open(char *file);
	bool save(char *contents);
	void close();
protected:
	char filename[24];
	ifstream in;
	ofstream out;

};

bool file::save(char *contents){
	if (!out){
		return false;
	}
	int i; for(i=0;contents[i];i++) out.put(contents[i]);
	return true;
}

bool file::open(char *file){
	in.close();
	out.close();
	strcpy(filename,file);
	in = ifstream(filename);
	out = ofstream(filename);
	if (!in | !out){
		return false;
	}
	return true;
}

void file::close(){
	in.close();
	out.close();
}
char *file::getcontents(){
	char c[MAX_CONT] = "";
	in.get(c,MAX_CONT);
	return c;
}
file::file(const char fileName[]){
	strcpy(filename,fileName);
	in = ifstream(filename);
	out = ofstream(filename);
}
what am i doing wrong?
Last edited on
You need to initialize in and out in the initializer list, like this:

1
2
file::file( const char fileName[] ) :
  filename( strdup( fileName ), in( filename ), out( filename ) {}


ifstream and ofstream objects are non-copyable. Your constructor is attempting to copy them.
I'm missing a ')' in the code above. Also, didn't see filename was declared as an array.

corrected:

1
2
3
4
5
file::file( const char fileName[] ) :
  in( fileName ), out( fileName )
{
  strncpy( filename, fileName, sizeof( filename ) );
}

found another problem: I do the same in file::open! How do I sort it there?
Last edited on
ifstream and ofstream both have open() methods you can call instead.

1
2
3
4
5
6
7
bool file::open( const char* file ) {
    close();
    strcpy( filename, file );
    in.open( filename );
    out.open( filename );
    return in && out;
}


BTW, your if() in open as written needs to use '||' not '|'.
ok new cide is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;

const int MAX_CONT = 400;

class file{			
public:
	file(const char fileName[]);
	file();
	char *getcontents();
	bool open(char *file);
	bool save(char *contents);
	void close();
protected:
	char filename[24];
	ifstream in;
	ofstream out;

};

bool file::save(char *contents){
	if (!out){
		return false;
	}
	int i; for(i=0;contents[i];i++) out.put(contents[i]);
	return true;
}

bool file::open(char *file){
	in.close();
	out.close();
	strcpy(filename,file);
	in.open(filename);
	out.open(filename);
	if (!in || !out){
		return false;
	}
	return true;
}

void file::close(){
	in.close();
	out.close();
}
char *file::getcontents(){
	char c[MAX_CONT] = "";
	in.get(c,MAX_CONT);
	return c;
}
file::file(const char fileName[]):
	in(fileName), out(fileName)
{
	strncpy(filename,fileName,sizeof(fileName));
}
But there are still 3 errors:

1>c:\program files\microsoft visual studio 9.0\vc\include\istream(846) : error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ios(152) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator =(const std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>c:\program files\microsoft visual studio 9.0\vc\include\fstream(548) : error C2248: 'std::basic_streambuf<_Elem,_Traits>::operator =' : cannot access private member declared in class 'std::basic_streambuf<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\streambuf(22) : see declaration of 'std::basic_streambuf<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_filebuf<_Elem,_Traits> &std::basic_filebuf<_Elem,_Traits>::operator =(const std::basic_filebuf<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>c:\program files\microsoft visual studio 9.0\vc\include\ostream(584) : error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ios(152) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
Last edited on
How are you using your file class?

Your file class is copy constructible but not assignable. This means that, for example, you could do something like this:

1
2
3
4
5
6
class my_other_class {
    public:
        my_other_class( const file& f ) : fil( f ) {}
    private:
        file fil;
};


but not this:

1
2
3
4
5
6
7
8
9
class my_other_class {
    public:
        my_other_class( const file& f ) {
             fil = f;  // Doesn't compile for same reason you had to change the other ctor
        }

    private:
        file fil;
};
ok, so I just needed to use file::open rather than =. But now there's problems with multiply defined functions when I try to test it...

1
2
3
4
5
6
7
1>file.obj : error LNK2005: "public: bool __thiscall file::save(char *)" (?save@file@@QAE_NPAD@Z) already defined in main.obj
1>file.obj : error LNK2005: "public: bool __thiscall file::open(char *)" (?open@file@@QAE_NPAD@Z) already defined in main.obj
1>file.obj : error LNK2005: "public: void __thiscall file::close(void)" (?close@file@@QAEXXZ) already defined in main.obj
1>file.obj : error LNK2005: "public: char * __thiscall file::getcontents(void)" (?getcontents@file@@QAEPADXZ) already defined in main.obj
1>file.obj : error LNK2005: "public: __thiscall file::file(char const * const)" (??0file@@QAE@QBD@Z) already defined in main.obj
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall file::file(void)" (??0file@@QAE@XZ) referenced in function _main
1>C:\Documents and Settings\Bradley\My Documents\Visual Studio 2008\Projects\Tutorials\Debug\Files.exe : fatal error LNK1120: 1 unresolved externals
Last edited on
I think the reason is because you put the implementation of your file class' methods in the header file, right? If so, you need to move them to a separate .cpp file. eg,

file.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef FILE_H
#define FILE_H

#include <fstream>

class file{			
public:
	file(const char fileName[]);
	file();
	char *getcontents();
	bool open(char *file);
	bool save(char *contents);
	void close();
protected:
	char filename[24];
	std::ifstream in;
	std::ofstream out;
};

#endif 


file.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "file.h"
#include <cstring>

using namespace std;

const int MAX_CONT = 400;


bool file::save(char *contents){
	if (!out){
		return false;
	}
	int i; for(i=0;contents[i];i++) out.put(contents[i]);
	return true;
}

bool file::open(char *file){
	in.close();
	out.close();
	strcpy(filename,file);
	in.open(filename);
	out.open(filename);
	if (!in || !out){
		return false;
	}
	return true;
}

void file::close(){
	in.close();
	out.close();
}
char *file::getcontents(){
	char c[MAX_CONT] = "";
	in.get(c,MAX_CONT);
	return c;
}
file::file(const char fileName[]):
	in(fileName), out(fileName)
{
	strncpy(filename,fileName,sizeof(fileName));
}

no difference, still says they're defined in main.obj
closed account (z05DSL3A)
Have you tried going into the project folder and manually deleting all the .obj files, then rebuilding?
Last edited on
ty that sorted errors with .obj files but now my functions don't actually do anything other than file::open.

Topic archived. No new replies allowed.