S
sandwich_eater
I am getting compiler errors using g++ in cygwin due to my lack of
knowledge of C++.
How do each of the following with std::string?
1. write a header for a function that takes a string (most efficient
way e.g. by reference), an integer and returns a string.
2. return a null std::string
3. see a documented list of functions implemented in std::string
4. make the function part of a class so that it extends the std::string
class
This is my first stab a LeftStr for starters as a test (i am not
passing by reference)
....
using namespace std;
string LeftStr(string s, int k)
{
if (k < 1)
{
return string "/0";
}
else if (s.length < k)
{
return s;
}
else
{
return s.substr (1, k);
}
}
compiler output
basic_str.cpp: In function `std::string LeftStr(std::string, int)':
basic_str.cpp:15: error: expected primary-expression before string
constant
basic_str.cpp:15: error: expected `;' before string constant
basic_str.cpp:17: error: invalid use of member (did you forget the `&'
?)
basic_str.cpp:32:2: warning: no newline at end of file
Thanks.
knowledge of C++.
How do each of the following with std::string?
1. write a header for a function that takes a string (most efficient
way e.g. by reference), an integer and returns a string.
2. return a null std::string
3. see a documented list of functions implemented in std::string
4. make the function part of a class so that it extends the std::string
class
This is my first stab a LeftStr for starters as a test (i am not
passing by reference)
....
using namespace std;
string LeftStr(string s, int k)
{
if (k < 1)
{
return string "/0";
}
else if (s.length < k)
{
return s;
}
else
{
return s.substr (1, k);
}
}
compiler output
basic_str.cpp: In function `std::string LeftStr(std::string, int)':
basic_str.cpp:15: error: expected primary-expression before string
constant
basic_str.cpp:15: error: expected `;' before string constant
basic_str.cpp:17: error: invalid use of member (did you forget the `&'
?)
basic_str.cpp:32:2: warning: no newline at end of file
Thanks.