Getting error while trying to copy STL Set into Vector - c++

I am trying to initialize vector by already preset Set but getting error
vector<string> findRepeatedDnaSequences(string s) {
int size = s.length();
set<string>container;
unordered_map<string,int>hmap;
for(int i=0;i<size;i++){
string first=s.substr(i,10);
if(hmap.find(first)!=hmap.end()){
// if((i-hmap[first])>=10)
container.insert(first);
}else hmap[first] = i;
}
// ERROR IN THIS LINE
return vector<string>res(container.begin(), container.end());
}
anagrams.cpp:21:26: error: expected '(' for function-style cast or type construction return vector<string>res((container.begin(), container.end()));

Related

access private member variable through private member, same class

// M9P369.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
const int MaxSize = 100;
using namespace std;
class Set {
int len; // number of members
char members[MaxSize]; // the set is stored in this array
int find(char ch); // find an element
public:
Set() { len = 0; } // constructor to make a null set initially
int getLength() { return len; } // return number of elements in the set
void showset(); // display the set
bool isMember(char ch); // check for membership
Set operator+(char ch); // overload operator to add an element to the set
Set operator-(char ch); // overload operator to remove an element from the set
Set operator+(Set ob2); // set Union - overloaded by the different type from above overload+ function
Set operator-(Set ob2); // set difference same as above.
};
// Return the index of the element passed in, or -1 if nothing found.
int Set::find(char ch) {
int i;
for (i=0; i < len; i++)
if (members.[i] == ch) return i;
return -1;
}
// Show the set
void Set::showset() {
cout << "{ ";
for (int i=0; i<len; i++)
cout << members[i] << " ";
cout << "}\n";
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
I am learning operator overloading, and came across a class access problem.
The line
if (members.[i] == ch) return i;
Gives a tooltip error 'expression must have class type', and compile errors:
\m9p369.cpp(34): error C2059: syntax error : '['
\m9p369.cpp(40): error C2228: left of '.showset' must have class/struct/union
\m9p369.cpp(41): error C2228: left of '.cout' must have class/struct/union
I am defining the private member function find() of class Set, and I get the error upon trying to access the private member char array of the same class, members. Error seems to say I should specify which class it's referring to, why? I already specify the class in the definition:
int Set::find(char ch) {
As I understand, members should be in the scope of the function definition. I looked hard for any stray characters I couldn't find anything odd, all parenthesis seem to match.
Problem is here:
members.[i]
It should be just
members[i]
Remeove the . from
if (members.[i] == ch) return i;
~~~~~~~~~~~^

push_back vector of pointers of pointers

std::vector<BPatch_point *> *points;
points = functions[0]->findPoint(BPatch_entry);
if ((*points).size() == 0)
{
cout << "Failed to get the points" << endl;
}
std::vector<ldframework::Point *> *new_points;
for(unsigned int i = 0; i < points->size(); i++ )
{
ldframework::Point *pt1 = new ldframework::PointImpl((*points[0]));
new_points->push_back(pt1);
}
The PointImpl constructor is :
ldframework::PointImpl::PointImpl(const BPatch_point&* po)
{
Bpatch_point *_bpoint=new BPatch_point(*po);
}
WHile compiling im getting following error. Could you please help
>BfunctionImpl.cpp: In member function âvirtual const std::vector<ldframework::Point*, std::allocator<ldframework::Point*> >* ldframework::BfunctionImpl::find_Point(ldframework::locClass)â:
BfunctionImpl.cpp:86: error: expected type-specifier
BfunctionImpl.cpp:86: error: cannot convert âint*â to âldframework::Point*â in initialization
BfunctionImpl.cpp:86: error: expected â,â or â;â
THe 86 line number code is
ldframework::Point *pt1 = new ldframework::PointImpl((*points[0]));
Have a look at the (*points[0]) part of line 86.
Since points is a pointer to a vector and the array subscript operator [] has a higher precedence than the unary indirection operator *, this is parsed as *(*(points+0)).
You might try to replace this by (*points)[0] or maybe even: (*points)[i]
Furthermore, new_points is a pointer to a vector, that has not been assigned to a pointer to a valid object before it is used.
Additionally, I see that you use a lot of pointers, which causes extra indirections, decrease of locality, and gives you the burden of cleaning-up things or causing memory leaks. Does new_points have to be a vector of pointers to ldframework::Point? You could use a vector of std::unique_ptr<ldframework::Point> to avoid the clean-up burden, but you can also consider:
std::vector<ldframework::PointImpl> *new_points =
new std::vector<ldframework::PointImpl>;
for(unsigned int i = 0; i < points->size(); i++ )
{
new_points->push_back( ldframework::PointImpl((*points)[i]) );
}
or even better:
std::vector<ldframework::PointImpl> new_points;
for(unsigned int i = 0; i < points->size(); i++ )
{
new_points.push_back( ldframework::PointImpl((*points)[i]) );
}

make_pair(string, class): error: expected primary-expression before â)â token

error: expected primary-expression before â)â token
I'm not entirely sure what's going on here, since my friends also working on this project can't seem to tell what's wrong. Any help on this error would be appreciated. The line that the error is referring to has a comment on it pointing it out. I'm trying to insert a pair into a map by the code below.
theCandidates is a map<string, class>, and in this case, that class is called Candidate.
void TallyVotes::initialize(Scanner& inStream)
{
numberOfLosers = 0;
numberOfVotes = boost::lexical_cast<int>(inStream.next());
numberOfCandidates = boost::lexical_cast<int>(inStream.next());
for(int i = 0; i < numberOfVotes ; i++)
{
for(int j = 0; j < numberOfCandidates ; i++)
{
theVotes[i][j] = inStream.next();
cand = theVotes[i][j];
if(i == 0)
{
theCandidates.insert(make_pair(cand, Candidate));//ERROR ON THIS LINE
}
}
}
} // void TallyVotes::initialize(Scanner& inStream)
The make_pair function takes two values as arguments, not a value and a type.
Try e.g.
make_pair(cand, Candidate())
// Note parentheses ^^
The expression Candidate() create a temporary object, which is then copied into the std::map.

Pointer in a pair in C++

I need to return an array and it's size:
pair<int*, int> load(string path,int * memory){
ifstream handle;
int container;
int size = 0;
if(!handle){
QMessageBox::warning(0, QString(""), "file cannot be loaded");
return make_pair(NULL,-1);
}
handle.open(path.c_str());
while(!handle.eof()){
handle >> container;
size++;
}
size--;
if(!size){
QMessageBox::warning(0, QString(""), "file is empty");
return make_pair(NULL,-1);
}
memory = new int[size];
handle.clear();
handle.seekg(0, ios::beg);
for(int i = 0; i < size; i++){
handle >> memory[i];
}
handle.close();
return make_pair(memory, size);
}
The error output is:
/usr/include/c++/4.6/bits/stl_pair.h:109: error: invalid conversion
from 'int' to 'int*' [-fpermissive]
How do I do it?
Since NULL is probably defined as:
#define NULL 0
the expression make_pair(NULL,-1) becomes make_pair(0, -1), so it creates pair<int, int>. You can for example use nullptr if available or (int*)NULL otherwise.

Trying to make my own string class

These are the errors I'm getting for my program.
myString1.cpp: In constructor ‘MyString1::MyString1(char*, int)’:
myString1.cpp:6: error: expected primary-expression before ‘]’ token
myString1.cpp:6: error: expected primary-expression before ‘]’ token
myString1.cpp: In member function ‘MyString1 MyString1::append(MyString1)’:
myString1.cpp:11: error: invalid use of member (did you forget the ‘&’ ?)
myString1.cpp: In member function ‘void MyString1::clear()’:
myString1.cpp:25: error: expected primary-expression before ‘]’ token
myString1.cpp:25: error: expected primary-expression before ‘{’ token
myString1.cpp:25: error: expected `;' before ‘{’ token
myString1.cpp: In member function ‘bool MyString1::empty()’:
myString1.cpp:29: error: expected primary-expression before ‘]’ token
myString1.cpp:31: error: expected primary-expression before ‘else’
myString1.cpp:31: error: expected `;' before ‘else’
And here is my program in the three different parts.
myString1.h
#ifndef MYSTRING1_H
#define MYSTRING1_H
class MyString1
{
private:
char chars[];
int size;
public:
MyString1();
MyString1(char chars[], int size);
MyString1 append(MyString1 s);
char at(int index);
int length();
void clear();
bool empty();
int find(char ch);
};
#endif
myString1.cpp
#include "myString1.h"
using namespace std;
MyString1::MyString1(char chars[], int size)
{
this->chars[] = chars[];
this->size = size;
}
MyString1 MyString1::append(MyString1 s)
{
for(int i = size; i > size - s.length; i++)
chars[i] = s.at(i);
}
char MyString1::at(int index)
{
return chars[index];
}
int MyString1::length()
{
return size;
}
void MyString1::clear()
{
size = 0;
chars[] = {};
}
bool MyString1::empty()
{
if(chars[]){
return true;
else
return false;
}
}
int MyString1::find(char ch)
{
for(int i = 0; i < size; i++)
if(chars[i] = ch)
return i;
}
testMyString1.cpp
#include <iostream>
#include "myString1.h"
using namespace std;
int main()
{
MyString1 first("cat", 4);
MyString1 second("dog", 4);
cout << first.at(1) << " and " << second.at(1) << endl;
first.append(second);
cout << first.at(6) << endl;
return 0;
}
Im a newbie just trying to learn how to use the g++ compiler so just looking for some help reading the error messages and debugging my code. Also I'm sure there is some very bad code so any help is appreciated.
The code has a lot of mistakes so I don't know where to start from but I suppose that generally giving you some pointers to help you with understanding your code would be okay.
In my opinion you don't need to have a size index in a String class since there is the strlen() function that will gladly compute it for you.
Now for your class declaration check how you declare the pointer that will hold the string for you. You need to do it like below:
class MyString1
{
private:
char* chars;//this declares a pointer to a char that will hold the string for you
public:
...
Also you are never allocating the char* that holds the strings. Your constructor should be:
MyString1::MyString1(const char* chars)
{
this->chars = (char*) malloc(strlen(chars)+1); //this will allocate an array of strlen() chars +1
strcpy(this->chars,chars);
}
As you can see I am not using a size index since strlen can quite efficiently find that out for you. +1 is for the '\0' that signifies the end of a string.
Now to append something to the string, that's gonna be tricky.
void MyString1::append(const MyString1& s) //it's good to give a constant reference to the string here
{
//first of all we gotta reallocate the pointer,since we don't have enough memory for the string
int newsize = strlen(this->chars) + strlen(s)+1;
this->chars = (char*) realloc(this->chars,newSize); \\ no check for realloc failure, I know but this is just an example
strcat(this->chars,s.chars);
}
You don't need to return anything when you append. You are doing something to THIS string.
Your ::at() function is almost okay. Imagine though what would happen if the size of the string was 10 and you request MyString1::at(12). This would probably cause a Segmentation fault (that's not good).
So you should alter your code to do bounds checking like below:
char MyString1::at(int index)
{
//if it's out of bounds let's return -1 which will signify that we got an out of bounds value (could also throw an exception here but that's a different subject altogether)
if(index > strlen(this->chars) || index <0)
return -1;
return chars[index];
}
Also in C/C++ you have to free the memory that you allocate. So in order to do that you should declare something called a destructor
MyString1::~MyString1()
{
free(this->chars);
}
Finally the is empty function can just be like that:
bool MyString1::empty()
{
return (this->chars[0] == '\0';
}