By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

code syntax

P: n/a

the code below is a close parallel to the code which is generating an
error I am trying to fix. the error is refereeing to the line in the
code marked <<<<<<<<<<<<<<.
can you please tell if the code is correct?

thanks

#include <string>
using std::string;
#include <vector>
using std::vector;
#include <fstream>
using std::ofstream;

class Ot {
static ofstream combo;
ofstream deta_f;
void some_method();

public:
Ot::Ot(string s){
combo.open( "file.txt", ios::app );
deta_f.open( s.c_str(), ios::app );
}

void Ot::some_method(){
deta_f << "...";
combo << "...";
}
};

class Grd {
Ot ot;
public:
Grd(string s):ot(s){}
};

class Grp {
vector<Grdgrds;
void group_them(){
Grd grd("something");
grds.push_back(grd); //<<<<<<<<<<<<<<<<<<<<<
}
public:
Grp(){}
~Grp(){}
};
Dec 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Gary Wessle wrote:
the code below is a close parallel to the code which is generating an
error I am trying to fix. the error is refereeing to the line in the
code marked <<<<<<<<<<<<<<.
can you please tell if the code is correct?
In the future, sharing the error message with the rest of us would be
helpful.
>
thanks

#include <string>
using std::string;
#include <vector>
using std::vector;
#include <fstream>
using std::ofstream;

class Ot {
static ofstream combo;
ofstream deta_f;
void some_method();

public:
Ot::Ot(string s){
combo.open( "file.txt", ios::app );
deta_f.open( s.c_str(), ios::app );
}

void Ot::some_method(){
deta_f << "...";
combo << "...";
}
};

class Grd {
Ot ot;
public:
Grd(string s):ot(s){}
};

class Grp {
vector<Grdgrds;
void group_them(){
Grd grd("something");
grds.push_back(grd); //<<<<<<<<<<<<<<<<<<<<<
The value type of a vector must be default constructible. Since you've
supplied your own constructor for class Grd the compiler does not
provide you a default constructor.

Mark
}
public:
Grp(){}
~Grp(){}
};
Dec 20 '06 #2

P: n/a
On 21 Dec 2006 09:30:21 +1100 in comp.lang.c++, Gary Wessle
<ph****@yahoo.comwrote,
>the code below is a close parallel to the code which is generating an
error I am trying to fix.
The error is accompanied by a friendly helpful message from the compiler
telling you what is wrong. Read it.
Dec 21 '06 #3

P: n/a
Gary Wessle <ph****@yahoo.comwrites:
the code below is a close parallel to the code which is generating an
error I am trying to fix. the error is refereeing to the line in the
code marked <<<<<<<<<<<<<<.
can you please tell if the code is correct?

thanks

#include <string>
using std::string;
#include <vector>
using std::vector;
#include <fstream>
using std::ofstream;

class Ot {
static ofstream combo;
ofstream deta_f;
void some_method();

public:
Ot::Ot(string s){
combo.open( "file.txt", ios::app );
deta_f.open( s.c_str(), ios::app );
}

void Ot::some_method(){
deta_f << "...";
combo << "...";
}
};

class Grd {
Ot ot;
public:
Grd(string s):ot(s){}
};

class Grp {
vector<Grdgrds;
void group_them(){
Grd grd("something");
grds.push_back(grd); //<<<<<<<<<<<<<<<<<<<<<
}
public:
Grp(){}
~Grp(){}
};
just some fixes but not compiling still

************************************************** **************

#include <string>
using std::string;
#include <vector>
using std::vector;
#include <fstream>
using std::ofstream;
using std::ios;

class Ot {
static ofstream combo;
ofstream deta_f;
public:
Ot(string s){
combo.open( "file.txt", ios::app );
deta_f.open( s.c_str(), ios::app );
}

void some_method(){
deta_f << "...";
combo << "...";
}
};

class Grd {
Ot ot;
public:
Grd(string s):ot(s){}
};

class Grp {
vector<Grdgrds;
void group_them(){
Grd grd("something");
grds.push_back(grd);
}
public:
Grp(){}
~Grp(){}
};
************************************************** **************
Dec 21 '06 #4

P: n/a
Gary Wessle :
Gary Wessle <ph****@yahoo.comwrites:
>the code below is a close parallel to the code which is generating an
error I am trying to fix. the error is refereeing to the line in the
code marked <<<<<<<<<<<<<<.
can you please tell if the code is correct?

thanks

#include <string>
using std::string;
#include <vector>
using std::vector;
#include <fstream>
using std::ofstream;

class Ot {
static ofstream combo;
ofstream deta_f;
void some_method();

public:
Ot::Ot(string s){
combo.open( "file.txt", ios::app );
deta_f.open( s.c_str(), ios::app );
}

void Ot::some_method(){
deta_f << "...";
combo << "...";
}
};

class Grd {
Ot ot;
public:
Grd(string s):ot(s){}
};

class Grp {
vector<Grdgrds;
void group_them(){
Grd grd("something");
grds.push_back(grd); //<<<<<<<<<<<<<<<<<<<<<
}
public:
Grp(){}
~Grp(){}
};

just some fixes but not compiling still

************************************************** **************

#include <string>
using std::string;
#include <vector>
using std::vector;
#include <fstream>
using std::ofstream;
using std::ios;

class Ot {
static ofstream combo;
ofstream deta_f;
public:
Ot(string s){
combo.open( "file.txt", ios::app );
deta_f.open( s.c_str(), ios::app );
}

void some_method(){
deta_f << "...";
combo << "...";
}
};

class Grd {
Ot ot;
public:
Grd(string s):ot(s){}
};

class Grp {
vector<Grdgrds;
void group_them(){
Grd grd("something");
grds.push_back(grd);
}
public:
Grp(){}
~Grp(){}
};
************************************************** **************
What is the error please?
Dec 21 '06 #5

P: n/a
Bo Yang wrote:
Gary Wessle :
>Gary Wessle <ph****@yahoo.comwrites:
>>the code below is a close parallel to the code which is generating
an error I am trying to fix. the error is refereeing to the line in
the code marked <<<<<<<<<<<<<<.
can you please tell if the code is correct?
[...]
************************************************* ***************

What is the error please?
If the compiler tries to create a copy c-tor (and it will if you don't
provide your own), it will fail since streams don't have copy c-tors.
Pushing a valud into a vector requires copy constrution. Try:

#include <fstream>
#include <vector>
class Boo {
std::fstream f;
public:
Boo(int);
};
int main() {
std::vector<Boofoo;
foo.push_back(Boo(42));
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 21 '06 #6

P: n/a

Gary Wessle wrote in message ...
>
just some fixes but not compiling still
Why did you not trim(remove) the old code?!?

See what Mr. Bazarov said. See my 'workaround' below....
>
************************************************* ***************
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <fstream>
using std::ofstream;
using std::ios;

class Ot {
static ofstream combo;
ofstream deta_f;
std::string ID;
>public:
// Ot(string s){
Ot( string s ) : ID( s ){
combo.open( "file.txt", ios::app );
deta_f.open( s.c_str(), ios::app );
}
Ot( Ot const &cpy ){}
~Ot(){
deta_f.close();
} // Dtor
Ot& operator=( Ot const &nm ){
if( &nm == this ){ return *this;}
ID = nm.ID;
if( not deta_f.is_open() ){
deta_f.clear();
deta_f.open( nm.ID.c_str(),
std::ios_base::out|std::ios_base::app );
} // if(!is_open)
return *this;
} // operator=(Ot const &)
// ---------------
// mote that is a candidate for exceptions. It could still fail.
// I'll let you deal with 'combo'.
>
void some_method(){
deta_f << "...";
combo << "...";
}
};

class Grd {
Ot ot;
public:
Grd(string s):ot(s){}
};

class Grp {
vector<Grdgrds;
void group_them(){
Grd grd("something");
grds.push_back(grd);
}
public:
Grp(){}
~Grp(){}
};
******************************************
In my tests, the copy constructor gets called with an empty argument, then
the assignment operator= is invoked with valid data argument.
[ adding to a 'map<string, OtMyMap; MyMap["aName"] = Ot("aName");'
approx..]
Try the changes I showed. If you have trouble, post:
1- minimal full program that demonstrates the problem.
2- the *exact* errors your compiler shows.
3- what it's doing or not doing, and what you expect.

--
Bob R
POVrookie
Dec 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.