not sure how to fix the error message i get

Nov 19, 2015 at 3:03am
im writing a program to display a tweet. it has a subject and message. i get a code error:
ec3.cpp: In member function ‘Tweet Twitter::gettweet(std::string)’:
ec3.cpp:60:15: error: ‘tweet’ was not declared in this scope
return tweet();
im not sure what i need to do to fix this error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//ec3.cpp

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Tweet
{public:
 Tweet(string s, string m);
 string getSubject();
 string getMessage();
 void changeMessage(string m);
private:
 string subject;
 string message;
};

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

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

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

void Tweet::changeMessage(string m)
{message = m;
}

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()
{Twitter t = Twitter();
 Tweet s = Tweet("FINISHED MIDTERM","ACED IT");
 t.addTweet(s);
}
Last edited on Nov 19, 2015 at 3:05am
Nov 19, 2015 at 3:44am
Nov 19, 2015 at 4:25am
i changed :
tweet() to Tweet()
but i still get the same error code
Nov 19, 2015 at 5:07am
do i need to make that:

return Tweet("finished midterm", "aced it");
Nov 19, 2015 at 8:50am
Pay attention to line 65 in your code. Now compare it to line 60 (along with the rest of the code from the function in line 54)

That function Tweet Twitter::gettweet(string subject) is declared as a type Tweet. Therefore, like all other functions with types other than void, it must return something of its type. What you did in your main was you declared objects of a certain type. You need to do the same in your respective functions (unless those objects are taken from the parameter lists).
Nov 19, 2015 at 2:12pm
When you write return Tweet();, that's saying you want to construct an object of type Tweet. Currently the only constructor is at lines 11/20.
20
21
22
23
Tweet::Tweet(string s, string m)
{subject = s;
 message = m;
}

Hence you need to either add a new constructor for the class which takes no parameters, or simply supply suitable values to the existing constructor.

Of course all of this raises the question - what is the correct action to take when gettweet() searches for a match, but finds nothing. Should it return an 'empty' tweet? Or perhaps the function should set some sort of error flag. Or maybe it should return an index or subscript which is set to a negative value when the search fails.

Last edited on Nov 19, 2015 at 2:12pm
Topic archived. No new replies allowed.