Can't figure out slight error

Mar 3, 2011 at 11:55pm
I'm getting the error:
Twitter.cpp: In function ‘int main()’:
Twitter.cpp:46: error: request for member ‘addtweet’ in ‘tw’, which is of non-class type ‘Twitter()’
Twitter.cpp:47: error: request for member ‘gettweet’ in ‘tw’, which is of non-class type ‘Twitter()’

If anyone can look at my code and let me know I would greatly appreciate it. Thank you in advance.


So here is what I have so far:

#include <string>
#include <iostream>
#include <vector>
#include "Tweet.h"
using namespace std;

class Twitter
{
public:
Twitter();
void addtweet(Tweet newmessage);
Tweet gettweet(string subject);

private:
vector<Tweet> messages;
};

Twitter::Twitter()
{

}


void Twitter::addtweet(Tweet newmessage)
{
messages.push_back(newmessage);
}

Tweet Twitter::gettweet(string subject)
{
for (int i=0; i< messages.size(); i++)
{
if (messages[i].getSubject()==subject)
{
return messages[i];
}
}
return Tweet();
}

int main()
{
Tweet t("Baseball", "Red Sox look good");
t.addstring(" - Real Good.");
Twitter tw();
tw.addtweet(t);
Tweet t2=tw.gettweet("Baseball");
cout << t2.getSubject() << endl;
cout << t2.getMessage() << endl;
}
Mar 3, 2011 at 11:57pm
Twitter tw();

That's a function declaration. Change it to Twitter tw; and you'll be fine. :)

-Albatross
Mar 4, 2011 at 12:02am
I changed it and got quite a few more errors. Do you know what to put in the default constructor Twitter::Twitter()? I left it blank because I didn't know what code to put in.
Mar 4, 2011 at 12:08am
f00tiefan wrote:
I changed it and got quite a few more errors

Could you be more specific?
Mar 4, 2011 at 12:14am
/tmp/ccNfccdU.o: In function `Twitter::gettweet(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Twitter.cpp:(.text+0x62): undefined reference to `Tweet::getSubject()'
Twitter.cpp:(.text+0xf2): undefined reference to `Tweet::Tweet()'
/tmp/ccNfccdU.o: In function `main':
Twitter.cpp:(.text+0x178): undefined reference to `Tweet::Tweet(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Twitter.cpp:(.text+0x263): undefined reference to `Tweet::addstring(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Twitter.cpp:(.text+0x3ab): undefined reference to `Tweet::getSubject()'
Twitter.cpp:(.text+0x405): undefined reference to `Tweet::getMessage()'
collect2: ld returned 1 exit status
Mar 4, 2011 at 12:21am
Well, what's inside Tweet.h? And where do you define the functions you declare inside Tweet.h?
Mar 4, 2011 at 12:44am
#ifndef TWEET_H
#define TWEET_H
#include <string>
using namespace std;


class Tweet
{
public:
Tweet();
Tweet(string s,string m);
string getSubject();
string getMessage();
void addstring(string add);

private:
string subject;
string message;
};

#endif
Mar 4, 2011 at 12:45am
Tweet::Tweet()
{
subject = "default_subject";
message = "default_name";
}

Tweet::Tweet(string s, string m)
{
subject = s;
message = m;
}

string Tweet::getSubject()
{
return subject;
}

string Tweet::getMessage()
{
return message;
}

void Tweet::addstring(string add)
{
message = message + add;
}
Mar 4, 2011 at 12:51am
Ok, my guess is that the things you just posted are two different files, Tweet.h and Tweet.cpp, and Tweet.cpp is not included in your project. I want you to try this: Copy the content of Tweet.cpp in Tweet.h, before the #endif directive, and try to compile Twitter.cpp (the one you have main in).
Mar 4, 2011 at 12:56am
In my default constructor I put this:

Twitter::Twitter()
{
messages.push_back(Tweet Def);
}

And got this error:

Twitter.cpp: In constructor ‘Twitter::Twitter()’:
Twitter.cpp:20: error: expected primary-expression before ‘Def’
Mar 4, 2011 at 1:03am
Don't put anything in your default constructor. Just leave it like this -> Twitter::Twitter() {}
Mar 4, 2011 at 1:14am
That fixed it. Thank you
Topic archived. No new replies allowed.