code syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    code syntax


    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(){}
    };
  • Mark P

    #2
    Re: code syntax

    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(){}
    };

    Comment

    • David Harmon

      #3
      Re: code syntax

      On 21 Dec 2006 09:30:21 +1100 in comp.lang.c++, Gary Wessle
      <phddas@yahoo.c omwrote,
      >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.


      Comment

      • Gary Wessle

        #4
        Re: code syntax

        Gary Wessle <phddas@yahoo.c omwrites:
        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(){}
        };
        *************** *************** *************** *************** ****

        Comment

        • Bo Yang

          #5
          Re: code syntax

          Gary Wessle :
          Gary Wessle <phddas@yahoo.c omwrites:
          >
          >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?

          Comment

          • Victor Bazarov

            #6
            Re: code syntax

            Bo Yang wrote:
            Gary Wessle :
            >Gary Wessle <phddas@yahoo.c omwrites:
            >>
            >>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<Boo foo;
            foo.push_back(B oo(42));
            }

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


            Comment

            • BobR

              #7
              Re: code syntax


              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_ba se::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


              Comment

              Working...