String not recognized in function definition - string

I am trying to code the number of occurences of 'e' in a particular string but it is giving me error while defining the function as it says string is not defined in this scope and occurence_e(which is my user-def function) to be not declared in this scope as well.
Following is the code:
#include<iostream>
#include<string.h>
void occurence_e(string);
using namespace std;
int main(){
string input;
cout<<"Enter any name"<<endl;
cin>>input;
occurence_e(input);
return 0;
}
void occurence_e(string input){
int count=0;
for (int i = 0; i < input.length(); i++)
{
if(input.at(i)=='e'){
count++;
}
}
cout<<"No. of times e comes in this name is "<<count;
}

The forward reference uses string, so move using namespace std; above it.
using namespace std;
void occurence_e(string);

Related

To find product of two largest no

#include<iostream>
#include<vector>
using namespace std;
int max(const vector<int>& num,int n)
{
int n_index=-1;
int m_index=-1;
for(int i=0;i<n;i++)
{
if(m_index==-1||num[m_index]<num[i])
m_index=i;
}
for (int i=0;i<n;i++)
{
if((i!=m_index) && (n_index==-1)||(num[n_index]<num[i]))
n_index=i;
}
int product=num[n_index]*num[m_index];
cout<<"output "<<num[m_index]<<" "<<num[n_index];
cout<<"product "<<product;
}
int main()
{
int n;
cout<<"enter the no ";
cin>>n;
vector<int>num(n);
for(int i=0;i<n;i++)
cin>>num[i];
max(num,n);
}
In the 2nd for loop in my max function after replacing "i" by "j" my code is working but if i use "i" ,why is it not working as "i" is local to that for loop ??
I have tried your code. It seems that the max function should be the non-return type.
void max(const vector<int>& num,int n)
{
...
}
And the program will work with 'i'.

Using c++11's std::async inside an abstract base class

Why doesn't making threads like this work inside of an abstract base class? I'm trying to abstract away all of the multithreading details for users who derive from this base class. I don't understand why it says "no type named 'type'" when I clearly write that callbackSquare returns type int.
#include <iostream>
#include <future>
#include <vector>
class ABC{
public:
std::vector<std::future<int> > m_results;
ABC(){};
~ABC(){};
virtual int callbackSquare(int& a) = 0;
void doStuffWithCallBack();
};
void ABC::doStuffWithCallBack(){
for(int i = 0; i < 10; ++i)
m_results.push_back(std::async(&ABC::callbackSquare, this, i));
for(int j = 0; j < 10; ++j)
std::cout << m_results[j].get() << "\n";
}
class Derived : public ABC {
Derived() : ABC() {};
~Derived(){};
int callbackSquare(int& a) {return a * a;};
};
int main(int argc, char **argv)
{
std::cout << "testing\n";
return 0;
}
The strange errors I'm getting are:
/usr/include/c++/5/future:1709:67: required from 'std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...) [with _Fn = int (ABC::*)(int&); _Args = {ABC*, int&}; typename std::result_of<_Functor(_ArgTypes ...)>::type = int]'
/usr/include/c++/5/future:1725:19: required from 'std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = int (ABC::*)(int&); _Args = {ABC*, int&}; typename std::result_of<_Functor(_ArgTypes ...)>::type = int]'
/home/taylor/Documents/ssmworkspace/callbacktest/main.cpp:16:69: required from here
/usr/include/c++/5/functional:1505:61: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<int (ABC::*)(int&)>(ABC*, int)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/5/functional:1526:9: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<int (ABC::*)(int&)>(ABC*, int)>'
_M_invoke(_Index_tuple<_Indices...>)
Your problem can be reproduced with any function that accepts a reference:
#include <future>
int f(int& a)
{
return a * a;
}
int main()
{
int i = 42;
auto r = std::async(f, i);
}
Accepting a reference in your code is risky since the variable will be modified by the loop iteration, creating a data race because the called function also accesses the variable.
Change the function to accept the input parameter by value, or call std::async by passing std::ref(i) or std::cref(i) (in case the function accepts a const reference) if you acknowledge the risk.

Error reading characters of string

I had a problem while trying to equal one string to another, I get this error "Error reading characters of string." in Visual Studio 2015, in the line that i am doing this "student_name=otherStudent.student_name;" and I just don't get it.
could you please help me?
Student& Student::operator=(const Student &otherStudent) {
if (this != &otherStudent) // Avoid self assignment
{
student_name=otherStudent.student_name;
for (int i = 0; i < grades_size; i++) {
grades[i] = otherStudent.grades[i];
subjet_names[i] = otherStudent.subjet_names[i];
}
average_grade = otherStudent.average_grade;
}
return *this;
}
Here is the definition of the class if it helps:
#include <iostream>
#include <string>
using namespace std;
#define grades_size 6
class Student {
private:
string student_name;
float grades[grades_size];
string subjet_names[grades_size];
float average_grade;
public:
//Constructor
Student();
Student(string name);
//Print status of Student
void printStudent();
//Operator Overloads
Student & operator=(const Student & otherStudent);
};

in my program when i write cin>>name[i] in class function its say expression must have pointer-to-object type

For that part name[i] it shows error. I want to store names of computer in a string array
using namespace std;
class computer
{
protected:
string name[100];
string model[100];
int prize[100];
string ram[100];
int limit;
public:
computer()
{
limit=0;
}
void name()
{
cout<<"Enter your desire records limit = ";
cin>>limit;
for(int i=0;i<limit;i++)
{
cout<<"Name = ";
cin>>name[i];
}
}
};
Change name[i] into name (name[i] means to let user enter a char)
and change the name() to getname()
Code:
using namespace std;
class computer
{
protected:
string name[100];
string model[100];
int prize[100];
string ram[100];
int limit;
public:
computer()
{
limit=0;
}
void getname()
{
cout<<"Enter your desire records limit = ";
cin>>limit;
for(int i=0;i<limit;i++)
{
cout<<"Name = ";
cin>>name;
}
}
};

How do I set up a function to convert vector of strings to vector of integers in VC++?

The question is in the title. Need help figuring out why my code compiles but doesn't work as intended. Thanks!
//This example demonstrates how to do vector<string> to vectro<int> conversion using a function.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
vector<int>* convertStringVectorToIntVector (vector<string> *vectorOfStrings)
{
vector<int> *vectorOfIntegers = new vector<int>;
int x;
for (int i=0; i<vectorOfStrings->size(); i++)
{
stringstream str(vectorOfStrings->at(i));
str >> x;
vectorOfIntegers->push_back(x);
}
return vectorOfIntegers;
}
int main(int argc, char* argv[]) {
//Initialize test vector to use for conversion
vector<string> *vectorOfStringTypes = new vector<string>();
vectorOfStringTypes->push_back("1");
vectorOfStringTypes->push_back("10");
vectorOfStringTypes->push_back("100");
delete vectorOfStringTypes;
//Initialize target vector to store conversion result
vector<int> *vectorOfIntTypes;
vectorOfIntTypes = convertStringVectorToIntVector(vectorOfStringTypes);
//Test if conversion is successful and the new vector is open for manipulation
int sum = 0;
for (int i=0; i<vectorOfIntTypes->size(); i++)
{
sum+=vectorOfIntTypes->at(i);
cout<<sum<<endl;
}
delete vectorOfIntTypes;
cin.get();
return 0;
}
The code above has only one problem: You are deleting your vectorOfStringTypes before you pass it to your conversion function.
Move the line delete vectorOfStringTypes; to after you have called your convert function and the program works as intended.

Resources