User forums > Using Code::Blocks

Initially cout works in header, but adding other cout causes error.

(1/1)

VicM:
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

--- End code ---

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)) ===|
--- End quote ---


--- 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

--- End code ---

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

AndrewCot:
Please read the info on the following page:
    http://forums.codeblocks.org/index.php/topic,9996.0.html

BugReporter:
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;
--- End code ---

EDIT: Also if you just started learning C++ you might want to bookmark the following:

* https://cplusplus.com/forum/
* https://en.cppreference.com/w/

VicM:
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

Navigation

[0] Message Index

Go to full version