C++ Classes/Vectors help!

This is a discussion on C++ Classes/Vectors help! within the C++ Programming forums, part of the General Programming Boards category; The main function is what was given to me, and I attempted to write the rest. It's pretty basic, but ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    16

    C++ Classes/Vectors help!

    The main function is what was given to me, and I attempted to write the rest. It's pretty basic, but I'm having trouble with the gettweet part.
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Tweet
    {
    public:
    	Tweet();
    	Tweet(string subject, string message);
    	string getsubject();
    	string getmessage();
    	void addtext(string x);
    private:
    	string subject;
    	string message;
    };
    Tweet::Tweet()
    {
    }
    Tweet::Tweet(string subject, string message)
    {
    	this->subject=subject;
    	this->message=message;
    }
    string Tweet::getsubject()
    {
    	return subject;
    }
    string Tweet::getmessage()
    {
    	return message;
    }
    void Tweet::addtext(string x)
    {
    	message += x;
    }
    
    class Twitter
    {
    public:
    	Twitter();
    	void addtweet(Tweet newmessage);
    	Tweet gettweet(string subject);
    private:
    	vector<Tweet> messages;
    };
    
    Twitter::Twitter()
    {
    }
    
    void Twitter::addtweet(Tweet newmessage)
    {
    	this->messages.push_back(newmessage);
    }
    
    Tweet Twitter::gettweet(string subject)
    {
    	for(int i=0;i<=messages.size();i++)
    	{
    		if(subject == messages[i].subject)
    		{
    			return messages[i];
    		}
    	}
    }
    
    
    
    int main()
    {
      string sub="Baseball";
      string mess="Red Sox look good.";
      Tweet t(sub,mess);
      t.addtext(" - Real Good.");
      Twitter tw();
      tw.addtweet(t);
      Tweet t2=tw.gettweet("Baseball");
      cout << t2.getsubject << endl;
      cout << t2.getmessage << endl;
    }
    How can I compare string subject to the subject inside messages[i]? Or am I doing this totally wrong?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    #1. If you're gonna use std::strings you should be including the <string> header.

    #2
    Code:
    Tweet Twitter::gettweet(string subject)
    {
    	for(int i=0;i<=messages.size();i++)
    	{
    		if(subject == messages[i].subject)
    		{
    			return messages[i];
    		}
    	}
    }
    What happens when the subject is not found? Your function is supposed to return a value, but in that case nothing is returned. Your compiler should be complaining (probably a warning) about this function, something along the lines of "not all control paths return a value".

    #3
    Code:
    class Tweet
    {
    public:
    	Tweet();
    	Tweet(string subject, string message);
    	string getsubject() const;
    	string getmessage() const;
    	void addtext(string x);
    private:
    	string subject;
    	string message;
    };
    
    ...
    
    string Tweet::getsubject() const
    {
    	return subject;
    }
    string Tweet::getmessage() const
    {
    	return message;
    }
    
    ...
    
    class Twitter
    {
    public:
    	Twitter();
    	void addtweet(Tweet newmessage);
    	Tweet gettweet(string subject) const;
    private:
    	vector<Tweet> messages;
    };
    
    ...
    
    Tweet Twitter::gettweet(string subject) const
    {
    Member functions that do not modify the class should be declared const.


    #4
    How can I compare string subject to the subject inside messages[i]? Or am I doing this totally wrong?
    Note that a Tweet object's subject member is private, so you can't directly access it in the Twitter::gettweet function as it stands in your current code. The Tweet objects do however have a handy getsubject member function that you should use. So, maybe...
    Code:
    if(subject == messages[i].getsubject())
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    Brilliant... thank you so much for this help!

    The final errors i'm getting are:

    Code:
    request for member ‘addtweet’ in ‘tw’, which is of non-class type ‘Twitter()’
    request for member ‘gettweet’ in ‘tw’, which is of non-class type ‘Twitter()’
    Does anyone know what these mean, and how to fix 'em?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Repost your code with the latest updates you've made.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class Tweet
    {
    public:
    	Tweet();
    	Tweet(string subject, string message);
    	string getsubject() const;
    	string getmessage() const;
    	void addtext(string x);
    private:
    	string subject;
    	string message;
    };
    Tweet::Tweet()
    {
    }
    Tweet::Tweet(string subject, string message)
    {
    	this->subject=subject;
    	this->message=message;
    }
    string Tweet::getsubject() const
    {
    	return subject;
    }
    string Tweet::getmessage() const
    {
    	return message;
    }
    void Tweet::addtext(string x)
    {
    	message += x;
    }
    
    class Twitter
    {
    public:
    	Twitter();
    	void addtweet(Tweet newmessage);
    	Tweet gettweet(string subject) const;
    private:
    	vector<Tweet> messages;
    };
    
    Twitter::Twitter()
    {
    }
    
    void Twitter::addtweet(Tweet newmessage)
    {
    	this->messages.push_back(newmessage);
    }
    
    Tweet Twitter::gettweet(string subject) const
    {
    	for(int i=0;i<=messages.size();i++)
    	{
    		if(subject == messages[i].getsubject())
    		{
    			return messages[i];
    		}
    	}
    }
    
    
    
    int main()
    {
    	string sub="Baseball";
    	string mess="Red Sox look good.";
    	Tweet t(sub,mess);
    	t.addtext(" - Real Good.");
    	Twitter tw();
    	tw.addtweet(t);
    	Tweet t2=tw.gettweet("Baseball");
    	cout << t2.getsubject() << endl;
    	cout << t2.getmessage() << endl;
    }

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by bigboybz View Post
    Brilliant... thank you so much for this help!

    The final errors i'm getting are:

    Code:
    request for member ‘addtweet’ in ‘tw’, which is of non-class type ‘Twitter()’
    request for member ‘gettweet’ in ‘tw’, which is of non-class type ‘Twitter()’
    Does anyone know what these mean, and how to fix 'em?

    The problem here comes in looking at the following three lines of code:
    Code:
    Twitter tw();
    tw.addtweet(t);
    Tweet t2=tw.gettweet("Baseball");
    The first line above does not create a Twitter object using the default constructor, but rather looks rather like a function prototype instead to your compiler. When creating an object using the default constructor, you do not use parenthesis. Since that line does not create an object with the name "tw", the errors on the following lines should be a bit more obvious. The solution is, as I mentioned, to not use those parenthesis when trying to declare the object tw:
    Code:
    Twitter tw;
    tw.addtweet(t);
    Tweet t2=tw.gettweet("Baseball");
    You still probably want to deal with the gettweet function not returning a value when the subject you are looking for isn't found.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    hmm, thats strange.. i assumed the int main was ok because it was given to me. But thanks so much for your help!!!

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    1
    Nah, Murphy makes lots of errors in the codes he usually gives us. The
    Code:
      cout << t2.getsubject() << endl;
      cout << t2.getmessage() << endl;
    didn't even have the () either. Also there was a ; missing somewhere else if I remember.

    Although I do the same thing and assume that it should function as he described.

    lol @ someone else being up this late working on hw.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    1
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    class Tweet
    {
    public:
    	Tweet();
    	Tweet(string subject, string message);
    	string getsubject() const;
    	string getmessage() const;
    	void addtext(string x);
    private:
    	string subject;
    	string message;
    };
    Tweet::Tweet()
    {
    }
    Tweet::Tweet(string subject, string message)
    {
    	this->subject=subject;
    	this->message=message;
    }
    string Tweet::getsubject() const
    {
    	return subject;
    }
    string Tweet::getmessage() const
    {
    	return message;
    }
    void Tweet::addtext(string x)
    {
    	message += x;
    }
    
    class Twitter
    {
    public:
    	Twitter();
    	void addtweet(Tweet newmessage);
    	Tweet gettweet(string subject) const;
    private:
    	vector<Tweet> messages;
    };
    
    Twitter::Twitter()
    {
    }
    
    void Twitter::addtweet(Tweet newmessage)
    {
    	this->messages.push_back(newmessage);
    }
    
    Tweet Twitter::gettweet(string subject) const
    {
    	for(int i=0;i<=messages.size();i++)
    	{
    		if(subject == messages[i].getsubject())
    		{
    			return messages[i];
    		}
    	}
    }
    
    
    
    int main()
    {
      string sub="Baseball";
      string mess="Red Sox look good.";
      Tweet t(sub,mess);
      t.addtext(" - Real Good.");
      cout<<t.getsubject()<<endl<<t.getmessage();
      cout<<"-------"<<endl;
      Twitter tw;
      tw.addtweet(t);
      Tweet t2=tw.gettweet("Baseball");
      cout << t2.getsubject() << endl;
      cout << t2.getmessage() << endl;
    }
    /gcc/mingw32/3.4.5

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21