Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Re Post of earlier message - ANSI String in Classes

Re Post of earlier message - ANSI String in Classes

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tom Moore
    wrote on last edited by
    #1

    Hi, I'm wondering if I can use the ANSI String Class in normal classes. I tried yesterday and got an error. Product.h

    #include "string.h"
    #include "iostream.h"

    using namespace std;
    class Product
    {
    public:
    Product(Product&);
    Product(string name, int amount , float price);
    float GetPrice();
    void SetPrice(float price);
    void SetAmount(int Amount);
    int GetAmount();
    void SetName(string name);
    string GetName();
    Product();
    virtual ~Product();
    private:
    float itsPrice;
    int itsAmount;
    string itsName;
    };

    Product.cpp

    Product::Product()
    {

    }

    Product::~Product()
    {

    }

    string Product::GetName()
    {
    return itsName;
    }

    void Product::SetName(string name)
    {
    itsName = name;
    }

    int Product::GetAmount()
    {
    return itsAmount;
    }

    void Product::SetAmount(int Amount)
    {
    itsAmount = Amount;
    }

    void Product::SetPrice(float price)
    {
    itsPrice = price;
    }

    float Product::GetPrice()
    {
    return itsPrice;
    }

    Product::Product(string name, int amount, float price)
    {
    itsName = name;
    itsAmount = amount;
    itsPrice = price;
    }

    Product::Product(Product &rhs)
    {
    itsName = rhs.GetName();
    itsPrice = rhs.GetPrice();
    itsAmount = rhs.GetAmount();
    }

    main.cpp

    #include "iostream.h"
    #include "Product.h"
    using namespace std;

    int main()
    {
    Product p1("Apple",0.25,50);

    cout << p1.GetName();
    int s;
    std::cin >> s;

    return 0;
    }

    The error is : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion) Please help. Thanks in advance Tom

    N 1 Reply Last reply
    0
    • T Tom Moore

      Hi, I'm wondering if I can use the ANSI String Class in normal classes. I tried yesterday and got an error. Product.h

      #include "string.h"
      #include "iostream.h"

      using namespace std;
      class Product
      {
      public:
      Product(Product&);
      Product(string name, int amount , float price);
      float GetPrice();
      void SetPrice(float price);
      void SetAmount(int Amount);
      int GetAmount();
      void SetName(string name);
      string GetName();
      Product();
      virtual ~Product();
      private:
      float itsPrice;
      int itsAmount;
      string itsName;
      };

      Product.cpp

      Product::Product()
      {

      }

      Product::~Product()
      {

      }

      string Product::GetName()
      {
      return itsName;
      }

      void Product::SetName(string name)
      {
      itsName = name;
      }

      int Product::GetAmount()
      {
      return itsAmount;
      }

      void Product::SetAmount(int Amount)
      {
      itsAmount = Amount;
      }

      void Product::SetPrice(float price)
      {
      itsPrice = price;
      }

      float Product::GetPrice()
      {
      return itsPrice;
      }

      Product::Product(string name, int amount, float price)
      {
      itsName = name;
      itsAmount = amount;
      itsPrice = price;
      }

      Product::Product(Product &rhs)
      {
      itsName = rhs.GetName();
      itsPrice = rhs.GetPrice();
      itsAmount = rhs.GetAmount();
      }

      main.cpp

      #include "iostream.h"
      #include "Product.h"
      using namespace std;

      int main()
      {
      Product p1("Apple",0.25,50);

      cout << p1.GetName();
      int s;
      std::cin >> s;

      return 0;
      }

      The error is : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion) Please help. Thanks in advance Tom

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      Instead of #include "string.h" #include "iostream.h" use #include #include


      My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

      T 1 Reply Last reply
      0
      • N Nemanja Trifunovic

        Instead of #include "string.h" #include "iostream.h" use #include #include


        My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

        T Offline
        T Offline
        Tom Moore
        wrote on last edited by
        #3

        I did! I typed in the '"' in the post because it wouldnt show in the preformatted way :confused: Tom

        N 1 Reply Last reply
        0
        • T Tom Moore

          I did! I typed in the '"' in the post because it wouldnt show in the preformatted way :confused: Tom

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

          I mean skip the ".h" part. #include NOT #include

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups