Author Topic: Initially cout works in header, but adding other cout causes error.  (Read 1455 times)

Offline VicM

  • Single posting newcomer
  • *
  • Posts: 6
Using CB 20.03 on 64-bit Win7 Pro box.  I'm attempting to learn programming in c++ and am using some of the examples in John Mueller's book.  The first header code is verbatim from the book.  The second header code is where I've entered one additional cout line in an attempt to modify eventually how the function works.
The following header works in the project.
Code
#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED

using namespace std;
enum Color
{
    blue,
    red,
    black,
    clear,
    grey
};
enum PenStyle
{
    ballpoint,
    felt_tim,
    fountain_pen
};

class Pen
{
public:
    Color InkColor;
    Color ShellColor;
    Color CapColor;
    PenStyle Style;
    float Length;
    string Brand;
    int InkLevelPercent;

    void write_on_paper(string words)
    {
      if(InkLevelPercent <= 0)
        {
            cout << "Oops! Out of ink!" << endl;
        }
        else
        {
            cout << words << endl;
            InkLevelPercent = InkLevelPercent - words.length();
        }
    }

    void break_in_half()
    {
        InkLevelPercent = InkLevelPercent/2;
        Length = Length/2.0;
    }

    void run_out_of_ink()
    {
        InkLevelPercent = 0;
    }
};
#endif // PEN_H_INCLUDED

But if I add even one cout line, I get a compile error I don't understand. The line throws the following error:

Quote
||=== Build: Debug in SomePens (compiler: GNU GCC Compiler) ===|
C:\Users\Vic\Documents\C++\SomePens\Pen.h||In member function 'void Pen::write_on_paper(std::__cxx11::string)':|
C:\Users\Vic\Documents\C++\SomePens\Pen.h|33|error: invalid use of member 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::length() const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long long unsigned int]' (did you forget the '&' ?)|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Code
#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED

using namespace std;
enum Color
{
    blue,
    red,
    black,
    clear,
    grey
};
enum PenStyle
{
    ballpoint,
    felt_tim,
    fountain_pen
};

class Pen
{
public:
    Color InkColor;
    Color ShellColor;
    Color CapColor;
    PenStyle Style;
    float Length;
    string Brand;
    int InkLevelPercent;

    void write_on_paper(string words)
    {
      /*Next line was added and causes error*/
      cout << "InkLevelPercent less words.length: " << InkLevelPercent - words.length << endl;

      if(InkLevelPercent <= 0)
        {
            cout << "Oops! Out of ink!" << endl;
        }
        else
        {
            cout << words << endl;
            InkLevelPercent = InkLevelPercent - words.length();
        }
    }

    void break_in_half()
    {
        InkLevelPercent = InkLevelPercent/2;
        Length = Length/2.0;
    }

    void run_out_of_ink()
    {
        InkLevelPercent = 0;
    }
};
#endif // PEN_H_INCLUDED

Why, in the original header do the cout lines work; but when I add one additional cout line it throws an error?

Thanks for any insights.

Vic

Offline AndrewCot

  • Lives here!
  • ****
  • Posts: 500
Re: Initially cout works in header, but adding other cout causes error.
« Reply #1 on: February 15, 2022, 03:47:39 am »
Please read the info on the following page:
    http://forums.codeblocks.org/index.php/topic,9996.0.html

Offline BugReporter

  • Single posting newcomer
  • *
  • Posts: 2
Re: Initially cout works in header, but adding other cout causes error.
« Reply #2 on: February 15, 2022, 06:33:11 pm »
It seems like you simply forgot to add parentheses to the length() function call:

Code
cout << "InkLevelPercent less words.length: " << InkLevelPercent - words.length << endl;
cout << "InkLevelPercent less words.length: " << InkLevelPercent - words.length() << endl;

EDIT: Also if you just started learning C++ you might want to bookmark the following:
« Last Edit: February 15, 2022, 06:38:11 pm by BugReporter »

Offline VicM

  • Single posting newcomer
  • *
  • Posts: 6
Re: Initially cout works in header, but adding other cout causes error.
« Reply #3 on: February 15, 2022, 09:33:09 pm »
Thank you both for your help.
It seems it was another user error situation. 
I sometimes find it difficult when using programs like CB where in certain instances, the IDE provides the parens and other times it doesn't.
I'll try to be more cognizant of those situations.
Vic