Sign in to follow this  
samurai_gator

C++ string::assign and using protected restriction keyword?

Recommended Posts

I'm unsure how to implement string::assign in Class Cat. I want the WhatDoestheMouseSay function to be of type string and return the string "EZ cheezy lemon squeezy" using the following in the latter function: string str; string base = "EZ cheezy lemon squeezy"; str.assign(base); cout << str << endl; I've been able to do it in my main, but is there a way to do it in the WhatDoestheMouseSay function or have it return variable cheese of type string? Also, my functions are listed using the protected restriction keyword. Why can't the rest of the class access them even though protected prevents functions outside the class from accessing variables? Thanks! Haven't seen this kind of error before. Errors void Cat::WhatDoestheMouseSay()' and `void Cat::WhatDoestheMouseSay()' cannot be overloaded|
[source/]

#include <iostream>
#include <string>

using namespace std;

class Cat
{
 public:
 void WhatDoestheMouseSay();



 void NineLives();

  Cat();
 ~Cat();

 protected:

   string cheese;
   int LOLcat;
  void WhatDoestheMouseSay();
  void NineLives();

 private:
};

Cat::Cat()
{
  cheese = " EZ cheezy lemon squeezy ";
}

Cat::~Cat()
{
}

void Cat::WhatDoestheMouseSay()
{

}

void Cat::NineLives()
{
  int number;
  cout << "How many LOLcats does it take to screw in a light bulb?" << endl;
  cin >> number;
  number *=10;
  cout << "Actually, it takes " << number << " of them to do it!" << endl;
}


int main()
{
  string str;
  string base = "EZ cheezy lemon squeezy";
  str.assign(base);
  cout << str << endl;

  Cat Gato;
  Gato.WhatDoestheMouseSay();
  Gato.NineLives();
  return 0;
}

Share this post


Link to post
Share on other sites
You've got two declarations for WhatDoestheMouseSay, one that's public and the other is protected.

Edit:
Do you mean something like this?

#include <string>
#include <iostream>

class foobar {
public:
foobar() {
mText = "123";
}

// returns a const-reference to mText, for the first and second examples
const std::string& text() const {
return mText;
}

// copies newString to mText, passed by const-reference; used in the first exapmle
void setText( const std::string& newString ) {
mText = text;
}

// returns a nonconst-reference to mText; for the second example
std::string& text() {
return mText;
}

private:
std::string mText;
};

int main() {
foobar f;

// 1:
std::cout << f.text(); // 123
f.setText( "abc" );
std::cout << f.text(); // abc

// 2:
f.text() = "qwertyuiop";
std::cout << f.text(); // qwertyuiop
}



Why not make "cheese" public?

Share this post


Link to post
Share on other sites
I've made "cheese" public and made a new function called Meow() that returns the string cheese. I'm not quite sure why function Meow() isn't returning a string. It's signature looks correct and I have no compilation errors.

I've copied and pasted the alternative solution you've provided and here are the errors I got:

\|17|error: no match for 'operator=' in '((foobar*)this)->foobar::mText = ((foobar*)this)->foobar::text'|

And some funky errors in basic_string.h also in the solution you've provided:

C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\basic_string.h|426|note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|

C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\basic_string.h|437|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|

C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\basic_string.h|451|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|

I'm not familiar with most of the syntax of the functions in main and how mText
works in the with the class even though it's private.

[source/]
#include <iostream>
#include <string>

using namespace std;

class Cat
{
public:
string cheese;
string Meow();
void NineLives();

Cat();
~Cat();

protected:
int LOLcat;

private:
};

Cat::Cat()
{
cheese = " EZ cheezy lemon squeezy ";
}

Cat::~Cat()
{
}



string Cat::Meow()
{
return cheese;
}

void Cat::NineLives()
{
int number;
cout << "How many LOLcats does it take to screw in a light bulb?" << endl;
cin >> number;
number *=10;
cout << "Actually, it takes " << number << " of them to do it!" << endl;
}


int main()
{
/*
string str;
string base = "EZ cheezy lemon squeezy";
str.assign(base);
cout << str << endl;
*/


Cat Gato;
Gato.NineLives();
Gato.Meow();
return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this