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.
#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:
||=== 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)) ===|
#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