string and class error

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

    string and class error

    i have created a class which contains all the information needed for a
    program based on accounts, this is shown below. When compiled the
    string "word" (in function writetofile) which is initialised in the
    constructor is apparently "undeclared ", yet it has been declared in
    the constructor.

    i am new to c++ class programming, do i need to also declare it, as
    well as initialise? and if not, what is the problem and how can it be
    solved?

    thanks

    wilson

    #include <iostream>
    #include <fstream>
    #include <time.h>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    class Account
    {
    friend void new_account();
    public:
    Account(){strin g word = "hello";}//can eventually be user
    inputted
    void returnbalance() { std::cout << balance <<
    std::endl; }
    void writetofile()
    {
    string extension = ".txt";
    ofstream writetofile;
    writetofile.ope n((word + extension).c_st r());
    writetofile << balance;
    }
    void deposit(float amount)
    {
    balance = balance + amount;
    std::cout << "$" << amount << " Has been deposited
    into your account";
    std::cout << std::endl << "Your balance is now $" <<
    balance;
    }
    virtual void withdraw(float amount)
    {
    if(amount < balance)
    {
    balance = balance - amount;
    std::cout << "$" << amount << "has been
    withdrawn from your account";
    std::cout << std::endl << "Your balance is
    now $" << balance;
    }
    else
    {
    std::cout << std::endl << "Insufficie nt funds,
    your balance is $" << balance;
    }
    }
    float balance;

    private:
    int pin_number;
    int account_number;


    };

  • John Harrison

    #2
    Re: string and class error

    Wilson wrote:
    i have created a class which contains all the information needed for a
    program based on accounts, this is shown below. When compiled the
    string "word" (in function writetofile) which is initialised in the
    constructor is apparently "undeclared ", yet it has been declared in
    the constructor.
    That's the problem. It's been declared in the constructor, but you
    should have declared it in the class, just like pin_number and
    account_number.

    If you declare something in a constructor, it is only visible in the
    constructor. If you declare it in a class, it is visible thoughout the
    class.


    john

    Comment

    • Wilson

      #3
      Re: string and class error

      On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
      Wilson wrote:
      i have created a class which contains all the information needed for a
      program based on accounts, this is shown below. When compiled the
      string "word" (in function writetofile) which is initialised in the
      constructor is apparently "undeclared ", yet it has been declared in
      the constructor.
      >
      That's the problem. It's been declared in the constructor, but you
      should have declared it in the class, just like pin_number and
      account_number.
      >
      If you declare something in a constructor, it is only visible in the
      constructor. If you declare it in a class, it is visible thoughout the
      class.
      >
      john
      thanks, however i had already done this, and i was provided with the
      messages "ISO C++ forbids the initialisation of member 'word'" and
      "making word static". These are given when the string is declared both
      as private (as is ideal, but not necesary) and public.

      is there a way to solve this?

      wilson

      Comment

      • =?ISO-8859-1?Q?Erik_Wikstr=F6m?=

        #4
        Re: string and class error

        On 2007-06-29 20:21, Wilson wrote:
        i have created a class which contains all the information needed for a
        program based on accounts, this is shown below. When compiled the
        string "word" (in function writetofile) which is initialised in the
        constructor is apparently "undeclared ", yet it has been declared in
        the constructor.
        >
        i am new to c++ class programming, do i need to also declare it, as
        well as initialise? and if not, what is the problem and how can it be
        solved?
        >
        thanks
        >
        wilson
        >
        #include <iostream>
        #include <fstream>
        #include <time.h>
        #include <cstdlib>
        #include <cstring>
        You really should bot be using <cstring>, use <stringinstread .
        <cstringis for C strings (char arrays/pointers) and using them is
        generally not a good idea.

        --
        Erik Wikström

        Comment

        • Andre Kostur

          #5
          Re: string and class error

          Wilson <tpwils@googlem ail.comwrote in news:1183141269 .145871.212830
          @q69g2000hsb.go oglegroups.com:
          i have created a class which contains all the information needed for a
          program based on accounts, this is shown below. When compiled the
          string "word" (in function writetofile) which is initialised in the
          constructor is apparently "undeclared ", yet it has been declared in
          the constructor.
          >
          i am new to c++ class programming, do i need to also declare it, as
          well as initialise? and if not, what is the problem and how can it be
          solved?
          >
          thanks
          >
          wilson
          >
          #include <iostream>
          #include <fstream>
          #include <time.h>
          #include <cstdlib>
          #include <cstring>
          using namespace std;
          class Account
          {
          friend void new_account();
          public:
          Account(){strin g word = "hello";}//can eventually be user
          Let's put a little whitespace in there:

          Account()
          {
          string word = "hello";
          }


          That's a local variable (no different than "extension" that you use
          below). It no longer exists once the constructor has completed.
          inputted
          void returnbalance() { std::cout << balance <<
          std::endl; }
          void writetofile()
          {
          string extension = ".txt";
          ofstream writetofile;
          writetofile.ope n((word + extension).c_st r());
          writetofile << balance;
          }

          Comment

          • Andre Kostur

            #6
            Re: string and class error

            Wilson <tpwils@googlem ail.comwrote in news:1183142845 .258465.40950
            @k29g2000hsd.go oglegroups.com:
            On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
            >Wilson wrote:
            i have created a class which contains all the information needed for a
            program based on accounts, this is shown below. When compiled the
            string "word" (in function writetofile) which is initialised in the
            constructor is apparently "undeclared ", yet it has been declared in
            the constructor.
            >>
            >That's the problem. It's been declared in the constructor, but you
            >should have declared it in the class, just like pin_number and
            >account_number .
            >>
            >If you declare something in a constructor, it is only visible in the
            >constructor. If you declare it in a class, it is visible thoughout the
            >class.
            >>
            >john
            >
            thanks, however i had already done this, and i was provided with the
            messages "ISO C++ forbids the initialisation of member 'word'" and
            "making word static". These are given when the string is declared both
            as private (as is ideal, but not necesary) and public.
            >
            is there a way to solve this?
            Show us the code.... we shouldn't have to guess at what you changed it
            to...

            Comment

            • Kai-Uwe Bux

              #7
              Re: string and class error

              Wilson wrote:
              i have created a class which contains all the information needed for a
              program based on accounts, this is shown below. When compiled the
              string "word" (in function writetofile) which is initialised in the
              constructor is apparently "undeclared ", yet it has been declared in
              the constructor.
              >
              i am new to c++ class programming, do i need to also declare it, as
              well as initialise? and if not, what is the problem and how can it be
              solved?
              >
              thanks
              >
              wilson
              >
              #include <iostream>
              #include <fstream>
              #include <time.h>
              #include <cstdlib>
              #include <cstring>
              using namespace std;
              class Account
              {
              friend void new_account();
              public:
              Account(){strin g word = "hello";}//can eventually be user
              inputted
              At this point, your compiler should bark about "string" being unknown. You
              did not include the header <stringthat provides std::string but the
              header <cstring>.

              [snip]


              Best

              Kai-Uwe Bux

              Comment

              • Wilson

                #8
                Re: string and class error

                On Jun 29, 8:02 pm, Andre Kostur <nntps...@kostu r.netwrote:
                Wilson <tpw...@googlem ail.comwrote in news:1183142845 .258465.40950
                @k29g2000hsd.go oglegroups.com:
                >
                >
                >
                >
                >
                On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
                Wilson wrote:
                i have created a class which contains all the information needed for a
                program based on accounts, this is shown below. When compiled the
                string "word" (in function writetofile) which is initialised in the
                constructor is apparently "undeclared ", yet it has been declared in
                the constructor.
                >
                That's the problem. It's been declared in the constructor, but you
                should have declared it in the class, just like pin_number and
                account_number.
                >
                If you declare something in a constructor, it is only visible in the
                constructor. If you declare it in a class, it is visible thoughout the
                class.
                >
                john
                >
                thanks, however i had already done this, and i was provided with the
                messages "ISO C++ forbids the initialisation of member 'word'" and
                "making word static". These are given when the string is declared both
                as private (as is ideal, but not necesary) and public.
                >
                is there a way to solve this?
                >
                Show us the code.... we shouldn't have to guess at what you changed it
                to...- Hide quoted text -
                >
                - Show quoted text -
                this is what i changed the code to, after removing it from the
                constructor. i am told that ISO C++ forbids the initialisation of
                "word", why is this? can it be solved?

                #include <iostream>
                #include <fstream>
                #include <time.h>
                #include <cstdlib>
                #include <cstring>
                using namespace std;
                class Account
                {
                friend void new_account();
                public:

                void returnbalance() { std::cout << balance <<
                std::endl; }
                void writetofile()
                {
                string extension = ".txt";
                ofstream writetofile;
                writetofile.ope n((word + extension).c_st r());
                writetofile << balance;
                }
                void deposit(float amount)
                {
                balance = balance + amount;
                std::cout << "$" << amount << " Has been deposited
                into your account";
                std::cout << std::endl << "Your balance is now $"
                <<
                balance;
                }
                virtual void withdraw(float amount)
                {
                if(amount < balance)
                {
                balance = balance - amount;
                std::cout << "$" << amount << "has been
                withdrawn from your account";
                std::cout << std::endl << "Your balance
                is
                now $" << balance;
                }
                else
                {
                std::cout << std::endl << "Insufficie nt funds,
                your balance is $" << balance;
                }
                }
                float balance;


                private:
                int pin_number;
                int account_number;
                string word = "hello";



                Comment

                • Robert Bauck Hamar

                  #9
                  Re: string and class error

                  Wilson wrote:
                  On Jun 29, 8:02 pm, Andre Kostur <nntps...@kostu r.netwrote:
                  >Wilson <tpw...@googlem ail.comwrote in news:1183142845 .258465.40950
                  >@k29g2000hsd.g ooglegroups.com :
                  >>
                  >>
                  >>
                  >>
                  >>
                  On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
                  >Wilson wrote:
                  i have created a class which contains all the information needed for
                  a program based on accounts, this is shown below. When compiled the
                  string "word" (in function writetofile) which is initialised in the
                  constructor is apparently "undeclared ", yet it has been declared in
                  the constructor.
                  >>
                  >That's the problem. It's been declared in the constructor, but you
                  >should have declared it in the class, just like pin_number and
                  >account_number .
                  >>
                  >If you declare something in a constructor, it is only visible in the
                  >constructor. If you declare it in a class, it is visible thoughout the
                  >class.
                  >>
                  >john
                  >>
                  thanks, however i had already done this, and i was provided with the
                  messages "ISO C++ forbids the initialisation of member 'word'" and
                  "making word static". These are given when the string is declared both
                  as private (as is ideal, but not necesary) and public.
                  >>
                  is there a way to solve this?
                  >>
                  >Show us the code.... we shouldn't have to guess at what you changed it
                  >to...- Hide quoted text -
                  >>
                  >- Show quoted text -
                  >
                  this is what i changed the code to, after removing it from the
                  constructor. i am told that ISO C++ forbids the initialisation of
                  "word", why is this? can it be solved?
                  >
                  #include <iostream>
                  #include <fstream>
                  #include <time.h>
                  #include <cstdlib>
                  #include <cstring>
                  You have used <string>. Include it:
                  #include <string>
                  using namespace std;
                  class Account
                  {
                  friend void new_account();
                  public:
                  >
                  void returnbalance() { std::cout << balance <<
                  std::endl; }
                  void writetofile()
                  {
                  string extension = ".txt";
                  ofstream writetofile;
                  writetofile.ope n((word + extension).c_st r());
                  writetofile << balance;
                  }
                  void deposit(float amount)
                  {
                  balance = balance + amount;
                  std::cout << "$" << amount << " Has been deposited
                  into your account";
                  std::cout << std::endl << "Your balance is now $"
                  <<
                  balance;
                  }
                  virtual void withdraw(float amount)
                  {
                  if(amount < balance)
                  {
                  balance = balance - amount;
                  std::cout << "$" << amount << "has been
                  withdrawn from your account";
                  std::cout << std::endl << "Your balance
                  is
                  now $" << balance;
                  }
                  else
                  {
                  std::cout << std::endl << "Insufficie nt funds,
                  your balance is $" << balance;
                  }
                  }
                  float balance;
                  >
                  >
                  private:
                  int pin_number;
                  int account_number;
                  string word = "hello";
                  You can't initialise things here. Just "string word;" will do. Initialise it
                  in the constructor:

                  Account() : word("hello") {}

                  --
                  rbh

                  Comment

                  • John Harrison

                    #10
                    Re: string and class error

                    Wilson wrote:
                    On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
                    >Wilson wrote:
                    >>i have created a class which contains all the information needed for a
                    >>program based on accounts, this is shown below. When compiled the
                    >>string "word" (in function writetofile) which is initialised in the
                    >>constructor is apparently "undeclared ", yet it has been declared in
                    >>the constructor.
                    >That's the problem. It's been declared in the constructor, but you
                    >should have declared it in the class, just like pin_number and
                    >account_number .
                    >>
                    >If you declare something in a constructor, it is only visible in the
                    >constructor. If you declare it in a class, it is visible thoughout the
                    >class.
                    >>
                    >john
                    >
                    thanks, however i had already done this, and i was provided with the
                    messages "ISO C++ forbids the initialisation of member 'word'" and
                    "making word static". These are given when the string is declared both
                    as private (as is ideal, but not necesary) and public.
                    >
                    is there a way to solve this?
                    >
                    wilson
                    >
                    Somehow I knew this was going to be the case.

                    You're are mixing up 'declaring' with 'assigning a value'

                    class Account
                    {
                    Account()
                    {
                    word = "hello"; // assign a value to word
                    }
                    string word; // declare word
                    };

                    john

                    Comment

                    • BobR

                      #11
                      Re: string and class error


                      Wilson <tpwils@googlem ail.comwrote in message...
                      i have created a class which contains all the information needed for a
                      program based on accounts, this is shown below. When compiled the
                      string "word" (in function writetofile) which is initialised in the
                      constructor is apparently "undeclared ", yet it has been declared in
                      the constructor.
                      No, you didn't! see below
                      >
                      i am new to c++ class programming, do i need to also declare it, as
                      well as initialise? and if not, what is the problem and how can it be
                      solved?
                      thanks, wilson
                      >
                      #include <iostream>
                      #include <fstream>
                      #include <time.h // remove this, you are not using it.
                      #include <cstdlib // remove this, you are not using it.
                      #include <cstring // remove this, you are not using it.
                      using namespace std;
                      // if this is a header, remove that!!
                      class Account{
                      friend void new_account(); // ???
                      public:
                      Account(){strin g word = "hello";}
                      //can eventually be user inputted

                      Where did you declare/define 'string'?
                      Hint: #include <string>

                      Account( std::string mystr = "hello" ){
                      std::string word = mystr; // or: word( mystr );
                      } // 'word' disappears right here.
                      void returnbalance() {
                      std::cout << balance <<std::endl;
                      }
                      Bad name. If you say 'return', then return something.

                      float ReturnBalance() const { // should use 'double'
                      return balance;
                      }

                      // void writetofile(){
                      // string extension = ".txt";
                      // ofstream writetofile;
                      // writetofile.ope n((word + extension).c_st r());

                      Note: 'word' is NOT defined here (it's not *in* your class).

                      void WriteToFile( std::string const &word ) /* const */ {
                      std::string filename( word );
                      filename += ".txt";
                      std::ofstream ofile( filename.c_str( ) );
                      if( not ofile.is_open() ){ /* error */ return;}
                      // ofile << balance;

                      WriteTo( ofile ); // see below
                      WriteTo( std::cout ); // see below
                      }
                      void WriteTo( std::ostream &out ) /* const */ {
                      if( not out ){ /* error */ return;}
                      out << balance;
                      } // now you can 'print' to any std ostream obj.
                      void deposit(float amount){
                      balance = balance + amount;
                      std::cout << "$" << amount
                      << " Has been deposited into your account";
                      std::cout << std::endl
                      << "Your balance is now $" << balance;
                      }
                      virtual void withdraw(float amount){
                      if(amount < balance){
                      balance = balance - amount;
                      std::cout << "$" << amount
                      << "has been withdrawn from your account";
                      std::cout << std::endl
                      << "Your balance is now $" << balance;
                      }
                      else{
                      std::cout << std::endl
                      <<"Insufficie nt funds, your balance is $"<<balance;
                      }
                      }
                      float balance;
                      private:
                      int pin_number;
                      int account_number;
                      };
                      >
                      Don't hesitate to ask if you need more help.

                      [corrections always welcome.]
                      --
                      Bob R
                      POVrookie


                      Comment

                      Working...