Pointers and class error

Hello, I am currently working on a program in which allows one to generate a new page after every 280 characters. In addition new page at 274 characters with no spaces and special characters as in punctuations. Yet I am getting errors with my pointers please help. :(
The current errors I am receiving are that:
Unqualified-id for the "Tweet = new class Tweet[100];"
also an undeclared identifier for 'arr'

This is my .cpp

#include "twitter.hpp"
void TwitterManager::splitTweets()
{
{
string message = message;
char s = ' ';
char e = '!';
char q = '?';
char p = '.';
int x = 0;
int counter = 0;
if((length) < 280)
{
*tweets[0] = message.substr(0,280);
*count = 1;
}
else
{
while (length > 0)
{
if(length < 279)
{
tweets[x] = message;
message.erase(0, length);
}
else if(message[279] == s)
{
tweets[x] = message.substr(0,280);
message.erase(0,280);
}
else if(message[279] == e)
{
tweets[x] = message.substr(0,280);
message.erase(0,280);
}
else if(message[279] == q)
{
tweets[x] = message.substr(0,280);
message.erase(0,280);
}
else if(message[279] == p)
{
tweets[x] = message.substr(0,280);
message.erase(0,280);
}
else
{
tweets[x] = message.substr(0,274);
message.erase(0,274);
}
if (tweets.rfind(p) > tweets.rfind(e) && tweets.rfind(p) > tweets.rfind(q) && tweets.rfind(p) > tweets.rfind(s) && tweets.rfind(p) < 280)
{
counter = tweets.rfind(p);
tweets[x] = message.substr(0,counter+1);
message.erase(0,counter+1);
}
else if (tweets.rfind(q) > tweets.rfind(e) && tweets.rfind(q) > tweets.rfind(p) && tweets.rfind(q) > tweets.rfind(s) && tweets.rfind(q) < 280)
{
counter = tweets.rfind(q);
tweets[x] = message.substr(0,counter+1);
message.erase(0,counter+1);
}
else if (tweets.rfind(e) > tweets.rfind(p) && tweets.rfind(e) > tweets.rfind(q) && tweets.rfind(e) > tweets.rfind(s) && tweets.rfind(e) < 280)
{
counter = tweets.rfind(e);
tweets[x] = message.substr(0,counter+1);
message.erase(0,counter+1);
}
else if (tweets.rfind(s) > tweets.rfind(e) && tweets.rfind(s) > tweets.rfind(q) && tweets.rfind(s) > tweets.rfind(p) && tweets.rfind(s) < 280)
{
counter = tweets.rfind(s);
tweets[x] = message.substr(0,counter+1);
message.erase(0,counter+1);
}
(x)++;
}
}
}
}
void TwitterManager::display()
{
int x = 1;
if (count > 1)
{
for (int i = 0; i < count; i++)
{
cout << "@" << username << ": " << arr[i] << " (" << x << "/" << count << ")";
cout << endl;
x++;
}
~Tweet(){};

}
else
{
cout << "@" << username << ": " << arr[0];
cout << endl;
}
~Tweet(){};
}

Here is my int main .cpp:

#include <iostream>
#include <string>
#include "twitter.hpp"

using namespace std;

int main()
{
string username;
string message;
cout << "Please enter your username: ";
cin >> username;
cin.ignore();
cout << "Please input your message: ";
getline(cin, message);
TwitterManager twe;
twe.setUsername(username);
twe.setMessage(message);
twe.display();
return 0;
}

And here is my .hpp file:

#ifndef TWITTER_HPP // This code prevents multiple includes of twitter.hpp
#define TWITTER_HPP

#include <iostream>
#include <string>
using namespace std;

class Tweet
{
private:
string username;
string message;
int charLength;
public:
void setUsername(string u)
{
username = u;
}
void setMessage(string m)
{
message = m;
}
string getUsername()
{
return username;
}
string getMessage()
{
return message;
}
Tweet()
{
username = "";
message = "";
charLength = 0;
}
Tweet(string u, string m)
{
username = u;
message = m;
charLength = m.length();
}
void display()
{
cout << "@" << username << ": " << message;
}
};

class TwitterManager
{
private:
string username;
string message;
Tweet* tweets[] = new tweets[length] ;
int length = 0;
void splitTweets();
public:
void setUsername(string u)
{
username = u;
}
void setMessage(string m)
{
message = m;
splitTweets();
}
string getUsername()
{
return username;
}
string getMessage()
{
return message;
}
TwitterManager()
{
username = "";
message = "";
length = 0;
Tweet = new class Tweet[100];
}
TwitterManager(string u, string m)
{
username = u;
message = m;
length = m.length();
Tweet = new class Tweet[100];
}
void display();
};
// Insert code here that creates the Tweet and TwitterManager classes.

#endif


Last edited on
Tweet* tweets[] = new tweets[length] ;

this line is wrong you do not need the [] braces after declaring a pointer so it would become the following line below,also be careful with capitals and lower case.

Tweet *tweets = new Tweet;

also

Tweet = new class Tweet[100];

you do not need to put the class keyword before Tweet this is creating a 100 new Tweets on the heap/free store memory,but this is wrong because you just specified the class name and not a variable name,

so it should be

Tweet *tweet = new Tweet[100]
Last edited on
Thank you I will try it.
Okay I did what you asked but after that I am getting more errors.
The errors I am now getting from my terminal are:
twitter.cpp; line 17; error: indirection requires pointer operand ('Tweet' invalid)
"*tweets[0] = message.substr(0,280);"

twitter.cpp; line 18; error: reference to overloaded function could not be resolved; did you mean to call it?
"*count = 1;"

twitter.cpp; line 28; error: no viable overloaded '='
"tweets[x] = message;"

class Tweet
twitter.hpp; line 8; note: candidate function (the
implicit copy assignment operator) not viable:
no known conversion from 'std::__1::string'
(aka 'basic_string<char, char_traits<char>,
allocator<char> >') to 'const Tweet' for 1st
argument

twitter.cpp; line 37; error: no viable overloaded '='
"tweets[x] = message.substr(0,280);"

twitter.hpp:8:7: note: candidate function (the
implicit move assignment operator) not viable:
no known conversion from
'std::__1::basic_string<char>' to 'Tweet' for
1st argument
class Tweet

twitter.cpp; line 43; error: no viable overloaded '='
"tweets[x] = message.substr(0,280);"

twitter.cpp; line 69; error: member reference type 'Tweet *' is a pointer; did you mean to use '->'?
"...&& tweets.rfind(p) > tweets.rfind(s) && tweet..."

twitter.cpp; line 69; error: no member named 'rfind' in 'Tweet'
error: member reference type 'Tweet *'
is a pointer; did you mean to use '->'?
"...> tweets.rfind(q) && tweets.rfind(p) > tweets..."
"...&& tweets.rfind(p) > tweets.rfind(q) && tweet..."
"...&& tweets.rfind(p) > tweets.rfind(q) && tweet..."
"...> tweets.rfind(e) && tweets.rfind(p) > tweets..."
"if (tweets.rfind(p) > tweets.rf..."
"if (tweets.rfind(p) > tweets.rf..."
"...if (tweets.rfind(p) > tweets.rfind(e) && twee..."

twitter.cpp; line 63; error: no viable overloaded '='
"...tweets[x] = message.substr(0,274);"

twitter.cpp; line 37: error: no viable overloaded '='
"tweets[x] = message.substr(0,280);"

twitter.cpp; line 55: error: no viable overloaded '='
"tweets[x] = message.substr(0,280);"


I never seen this error before:
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:1244:1: note:
possible target for call
count(_InputIterator __first, _InputIterator ...
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__bit_reference:307:1: note:
possible target for call
count(__bit_iterator<_Cp, _IsConst> __first, ...



Here is the code that I corrected.
#ifndef TWITTER_HPP // This code prevents multiple includes of twitter.hpp
#define TWITTER_HPP

#include <iostream>
#include <string>
using namespace std;

class Tweet
{
private:
string username;
string message;
int charLength;
public:
void setUsername(string u)
{
username = u;
}
void setMessage(string m)
{
message = m;
}
string getUsername()
{
return username;
}
string getMessage()
{
return message;
}
Tweet()
{
username = "";
message = "";
charLength = 0;
}
Tweet(string u, string m)
{
username = u;
message = m;
charLength = m.length();
}
void display()
{
cout << "@" << username << ": " << message;
}
};

class TwitterManager
{
private:
string username;
string message;
Tweet *tweets = new Tweet;
int length = 0;
void splitTweets();
public:
void setUsername(string u)
{
username = u;
}
void setMessage(string m)
{
message = m;
splitTweets();
}
string getUsername()
{
return username;
}
string getMessage()
{
return message;
}
TwitterManager()
{
username = "";
message = "";
length = 0;
Tweet *tweet = new Tweet[100];

}
TwitterManager(string u, string m)
{
username = u;
message = m;
length = m.length();
Tweet *tweet = new Tweet[100];
}
void display();
};
// Insert code here that creates the Tweet and TwitterManager classes.

#endif
Last edited on
Why don't you get rid of the pointer stuff and use a vector<Tweet> ?
In modern C++ there is rarely a need for dynamic memory.
post the all the code again in one post with all your cpp files (including the corrected code) and I will try help
Topic archived. No new replies allowed.