This code gives compiler warnings when I tried to compile it.
The first at line 65
|
for (int i = 0; i < longest.length(); ++i)
|
[Warning] comparison between signed and unsigned integer expressions [-Wsign-compare] |
That's easily remedied by changing
int i
to an unsigned type, such as
size_t
.
|
for (size_t i = 0; i < longest.length(); ++i)
|
Another compiler warning at line 91:
[Warning] no return statement in function returning non-void [-Wreturn-type] |
Though it is 'only' a warning, its impact is severe. The function promises to return an
intString
but doesn't do so.
Inserting the line
just before the closing brace at line 91 should resolve that.
I tested the program as follows:
1 2 3 4 5 6 7 8 9 10 11 12
|
int main()
{
intString a = 123;
intString b = 456;
std::cout << "a = " << a << '\n';
std::cout << "b = " << b << '\n';
intString c = a + b;
std::cout << "c = " << c << '\n';
}
|
This was the output:
The value of c is incorrect, it should be 579, not 1641.
It also gives the somewhat surprising result, 0 + 0 = 16
I've not attempted to debug the problem any further.
It is good practice to use
const
where appropriate. Also make the member variable
m_value
private.
1 2 3 4 5 6 7 8 9 10 11
|
class intString
{
std::string m_value;
public:
intString(int value = 0);
std::string getValue() const;
void setValue(std::string s);
friend intString operator+(const intString &s1, const intString &s2);
friend std::ostream& operator<<(std::ostream &out, const intString& val);
};
|