declaring vectors in scope - c++

In my In-Progress BigInt class, I am having trouble with the declaration of vectors. I am getting the errors:
prog.cpp: In function 'void setBig1(std::string, int)':
prog.cpp:45:3: error: 'big1' was not declared in this scope
big1.push_back(dig[x]);
^
prog.cpp: In function 'voidgetBig1(int)':
prog.cpp:50:11: error: 'big1' was not declared in this scope
cout << big1[x] ;
I believe that my getters and setters involved with the vector big1 are not recognizing the decleration of the vector in the 'public:' portion of the class definition. But I cannot find a solution or a definite reason for the errors. My code is Below:
//my bigInt class
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//class, constructors
//overload operators methods
//add, subtract, multiply, divide
class bigInt{//class
public:
bigInt();
~bigInt();
void setString(string dig);
string getString(void);
int getDigitLength(void);
std::string digit;
int digitLength;
std::string digString;
void setBig1(string dig, int dLength);
std::vector<int> big1;
std::vector<int> big2;
void getBig1(int dLength);
};
//constructors
bigInt::bigInt(void){
std::vector<int> big1;
}
//deconstructor
bigInt::~bigInt(){
}
//getters/setters
void bigInt::setString(string dig){
digit= dig;
digitLength= (digit.length());
}
string bigInt::getString(){
return digit;
}
int bigInt::getDigitLength(){
return digitLength;
}
void setBig1(string dig, int dLength){
for(int x= 0; x<(dLength); x++)
{
big1.push_back(dig[x]);
}
}
void getBig1(int dLength){
for(int x= 0; x<(dLength); x++){
cout << big1[x] ;
}
}
int main(){
string digString= "1"; //string
bigInt my_int{};
//bigInt big1<int>;
my_int.setString(digString); //setInternalString to equal digString
cout << my_int.getString() << endl; //prints internal string
my_int.setBig1(my_int.getString(), my_int.getDigitLength());//sets big1 vector = to string
my_int.getBig1(my_int.getDigitLength()); //print big1 vector
}
I greatly appreciate any assistance.

You forgot to specify class to which the member functions are defined. Instead of
void setBig1(string dig, int dLength){
for(int x= 0; x<(dLength); x++)
{
big1.push_back(dig[x]);
}
}
void getBig1(int dLength){
for(int x= 0; x<(dLength); x++){
cout << big1[x] ;
}
write
void bigInt::setBig1(string dig, int dLength){
for(int x= 0; x<(dLength); x++)
{
big1.push_back(dig[x]);
}
}
void bigInt::getBig1(int dLength){
for(int x= 0; x<(dLength); x++){
cout << big1[x] ;
}
Also you could declare all getters with qualifier const because they do not change an object itself of the class. For example
class bigInt
{
//...
void getBig1(int dLength) const;
};
void bigInt::getBig1(int dLength) const
{
for ( int i = 0; i < dLength; i++ ) cout << big1[i] ;
}
and in general case instead of type int there is better to use at least type size_t or std::vector::size_type
The vectors themselves could be declared with the access control private.

Related

Cannot convert - C++ - std::string__cxxll:string

#include <iostream>
#include <limits>
#include <string>
using namespace std;
void wczytajOsobe(string imie[], string nazwisko[], int wiek[])
{
int i=2;
for(int indeks=0;i>indeks;indeks++)
{
cout << "Podaj Imie: " << endl;
getline(cin, imie[indeks]);
cout << "Podaj Naziwsko: " << endl;
getline(cin, nazwisko[indeks]);
}
}
void wypiszOsobe(string imie[], string nazwisko[], int wiek[])
{
int i=2;
for(int indeks=0;i>indeks;indeks++)
{
cout << imie[indeks];
cout << nazwisko[indeks];
cout << wiek[indeks];
}
}
int main()
{
string imie[2];
string nazwisko[2];
int wiek[2];
for( int i = 0; i < 2; i++ )
wczytajOsobe(imie[i], nazwisko[i], wiek[i]);
for( int i = 0; i < 2; i++ )
wypiszOsobe(imie[ i ], nazwisko[ i ], wiek[ i ] );
system("PAUSE");
return 0;
}
This is my code and i have problem with|36|error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'std::__cxx11::string* {aka std::__cxx11::basic_string}' for argument '1' to 'void wczytajOsobe(std::__cxx11::string, std::__cxx11::string*, int*)'|
can somebody help me with that issue ?
You have two functions defined as:
void wczytajOsobe(string imie[], string nazwisko[], int wiek[]);
void wypiszOsobe(string imie[], string nazwisko[], int wiek[]);
Because arrays decay to pointers when passed to functions, the parameter types are actually:
void wczytajOsobe(string *imie, string *nazwisko, int *wiek);
void wypiszOsobe(string *imie, string *nazwisko, int *wiek);
When you call the functions like:
for( int i = 0; i < 2; i++ )
wczytajOsobe(imie[i], nazwisko[i], wiek[i]);
You're not passing arrays but individual array elements. That's why the error message says it can't convert std::string to std::string*.
You don't need those loops in main(). You can just call the functions as:
int main()
{
string imie[2];
string nazwisko[2];
int wiek[2];
wczytajOsobe(imie, nazwisko, wiek);
wypiszOsobe(imie, nazwisko, wiek);
system("PAUSE");
return 0;
}
Note that I'm just passing the arrays to the functions.

Identifier not found - error C3861 in Visual Studio 2019

I have a problem with my code. Unfortunately, when compiling I get these errors all the time. What can this be caused by and how to fix it?
error C3861: 'print': identifier not found
My code:
main.cpp
#include "pojazdy.h"
#include <iostream>
using namespace std;
int main()
{
Pojazdy** poj;
int size{ 0 }, index{ 0 };
Petla(poj, size);
print(poj, size);
wyrejestruj(poj,size,0);
print(poj, size);
wyrejestruj(poj,size);
return 0;
}
pojazdy.h
#ifndef pojazdy_h
#define pojazdy_h
#include <iostream>
#include <cstdlib>
using namespace std;
class Pojazdy
{
public:
string typ;
string marka;
string model;
string z_dod;
int ilosc;
int cena;
void dodaj();
void d_pojazd(Pojazdy**& pojazdy, int& size);
void wyrejestruj(Pojazdy**& pojazdy, int& size, int index);
void print(Pojazdy** pojazdy, int size);
void Petla(Pojazdy**& p, int& size);
//void wyswietl();
int get_ilosc() { return ilosc; }
string get_typ() { return typ; }
string get_marka() { return marka; }
string get_model() { return model; }
int get_cena() { return cena; }
void set_ilosc(int x);
};
#endif
pojazdy.cpp
#include "pojazdy.h"
#include <iostream>
using namespace std;
void Pojazdy::set_ilosc(int x) { ilosc = x; }
void Pojazdy::dodaj()
{
cout << "DODAWANIE POJAZDU..." << endl;
cout << "Podaj typ pojazdu:";
cin >> typ;
cout << "Podaj marke pojazdu: ";
cin >> marka;
cout << "Podaj model pojazdu: ";
cin >> model;
cout << "Dodaj cene pojazdu: ";
cin >> cena;
}
void Petla(Pojazdy**& p, int& size) {
char z_dod;// = 'N';
do {
d_pojazd(p, size); //odpowiada za dodawnie
p[size - 1]->dodaj();
cout << "Czy chcesz zakonczyc dodawanie? Jesli tak, wcisnij Y/N: ";
cin >> z_dod;
} while (z_dod == 'N' || z_dod == 'n');//while (p[size]->z_dod == "N" ||p[size]->z_dod == "n");
}
void print(Pojazdy** pojazdy, int size) {
std::cout << "====================================" << std::endl;
for (int i{ 0 }; i < size; i++)
std::cout << "Typ: " << pojazdy[i]->get_typ() << " Marka: " << pojazdy[i]->get_marka() << " Model: " << pojazdy[i]->get_model() << " Cena: " << pojazdy[i]->get_model() << std::endl;
}
void wyrejestruj(Pojazdy**& pojazdy, int& size) {
for (size_t i{ 0 }; i < size; i++)
delete pojazdy[i];
delete[] pojazdy;
size = 0;
pojazdy = NULL;
}
void wyrejestruj(Pojazdy**& pojazdy, int& size, int index) {
if (index < size) {
Pojazdy** temp = new Pojazdy * [size - 1];
short int j{ -1 };
for (size_t i{ 0 }; i < size; i++) {
if (i != index) {
j++;
temp[j] = pojazdy[i];
}
}
delete[] pojazdy;
--size;
pojazdy = temp;
}
else
std::cout << "Pamiec zwolniona!" << std::endl;
}
void d_pojazd(Pojazdy**& pojazdy, int& size) {
Pojazdy** temp = new Pojazdy * [size + 1];
if (size == 0)
temp[size] = new Pojazdy;
else {
for (int i{ 0 }; i < size; i++)
temp[i] = pojazdy[i];
delete[] pojazdy;
temp[size] = new Pojazdy;
}
++size;
pojazdy = temp;
}
I used #ifndef, #define, #endif and #pragma once, but none of them work. I will be really grateful for every code, I am already tired of this second hour. And forgive the non-English variables and function names for them - it's university code, so I didn't feel the need.
Move the functions below outside the class declaration.
void wyrejestruj(Pojazdy**& pojazdy, int& size, int index);
void print(Pojazdy** pojazdy, int size);
void Petla(Pojazdy**& p, int& size);
Or make them static and call like Pojazdy::print(poj, size);.
You declared a non-static member function print in the class definition
class Pojazdy
{
public:
// ...
void print(Pojazdy** pojazdy, int size);
//...
but you are trying to call it as a stand-alone function in main
print(poj, size);
So the compiler issues an error.
The declaration of the function as a stand alone function that at the same time is its definition in the file pojazdy.cpp is not visible in the module with main because this module includes only the header with the class declaration.
You should decide whether this function should be a member function of the class or a stand alone function.
You are not calling your member functions correctly. print can only be called on an object of type Pojazdy, so you need to do something like:
Pojazdy** poj;
int size{ 0 }, index{ 0 };
Pojazdy x; // Creates an object of Pojazdy called z
x.print(poj,size); // Calls the print method on x
Alternatively, if you don't want to have to declare an object, you could make the method static and just call it on the class.
In the .h file:
static void print(Pojazdy** pojazdy, int size);
And then in main:
Pojazdy** poj;
int size{ 0 }, index{ 0 };
Pojazdy::print(poj, size); // Calls the print method on the class
You put your function prototypes in the wrong place. They should be after the class decalration.
class Pojazdy
{
...
};
void print(Pojazdy** pojazdy, int size);
void wyrejestruj(Pojazdy**& pojazdy, int& size);
etc.
print is not a member of the Pojazdy class, so it's wrong to put the prototype inside the Pojazdy class declaration.

Unable to pass a function pointer to a member function of the class as an argument to another member function of the same class

I need help... appropriate questions have been asked in the comments. The programs has zero compiler errors and warnings!! I have concerns with calling a member function from another member function using function pointers. (To be precise, setMatrixto() is trying to call setElement() function using function pointer)
Plus somehow the "hello there" is not being printed to the console. I was expecting it to show up as output.Maybe the setMatrixto() is not getting called at all!!
Header File definition
#ifndef MATRIXOPERATIONS_H
#define MATRIXOPERATIONS_H
class MatrixOperations;
typedef int (MatrixOperations::*INTFUNC)(int,int);
typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);
class MatrixOperations
{
public:
MatrixOperations();
MatrixOperations(int size);
~MatrixOperations();
//diagonal matrix funtions
void displayMatrixOf(INTFUNC f);
void setMatrixTo(VOIDFUNC f);
int getElement(INTFUNC from, int i, int j);
void setElement(VOIDFUNC to,int i ,int j, int value);
int fromDiagonalArray(int i, int j);
void toDiagonalArray(int i, int j, int value);
protected:
private:
int size;
int* a;
};
#endif // MATRIXOPERATIONS_H
CPP Implementation File
#include "MatrixOperations.h"
#include <iostream>
using namespace std;
MatrixOperations::MatrixOperations()
{
//ctor
size = 3;
a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{
//ctor
this->size = size;
a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
//dtor
delete[] a;
}
///////////////////FUCNTION POINTER SECTION///////////////////////////////////
int MatrixOperations::getElement(INTFUNC from, int i, int j)
{
return (this->*from)(i,j);
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this->*to)(i,j,value);
}
/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
int MatrixOperations::fromDiagonalArray(int i, int j)
{
if(i==j)
{
return a[i];
}
else
{
return 0;
}
}
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
a[i] = value;
}
///////////////////////////////////////////////////////////////////
void MatrixOperations::displayMatrixOf(INTFUNC f)
{
for(int i{0}; i < size; i++)
{
for(int j{0}; j < size; j++)
{
cout << getElement(f,i,j) << "\t"; //is this the correct way to send the function pointer?
}
cout << endl;
}
}
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
cout << "Hello there!!"; //not getting this output.. whats wrong??
for(int i{0}; i < size; i++)
{
int value {};
cout << "Enter value diagonal element " << i << " : ";
cin >> value;
setElement(f,i,i,value); //is this the correct way to send the function pointer?
}
}
///////////////////////////////////////////////////////////////////////////////
Main File
#include <iostream>
#include "MatrixOperations.h"
typedef MatrixOperations MATRIX;
using namespace std;
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray); //was expecting a "Hello there!" but i am not getting that output either
return 0;
}
EDIT2: I added all the class definitions and main function in one single file. SURPRISINGLY!! this works . I am confused??!!!
#include <iostream>
using namespace std;
class MatrixOperations;
typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);
typedef MatrixOperations MATRIX;
class MatrixOperations
{
public:
MatrixOperations();
MatrixOperations(int size);
~MatrixOperations();
//diagonal matrix funtions
void setMatrixTo(VOIDFUNC f);
void setElement(VOIDFUNC to,int i ,int j, int value);
void toDiagonalArray(int i, int j, int value);
private:
int size;
int* a;
};
MatrixOperations::MatrixOperations()
{ //ctor
size = 3;
a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{ //ctor
this->size = size;
a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
//dtor
delete[] a;
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this->*to)(i,j,value);
}
/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
a[i] = value;
}
///////////////////////////////////////////////////////////////////
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
cout << "Hello there!!" << endl;
for(int i{0}; i < size; i++)
{
int value {};
cout << "Enter value diagonal element " << i << " : ";
cin >> value;
setElement(f,i,i,value);
}
}
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray);
return 0;
}
There is nothing wrong with the code in both cases. Its just my debugger was not running in admin mode. I got error code 740. So I launched my IDE in admin mode and voila it worked.

c++: vector of class instances, search by class member values failed

I have the following program where I defined a vector of class Point. I pushed into this vector five Point instances, with their Ids. Then I tried to search by Id but didn't get the expected result. The following program didn't return anything.
#include<iostream>
#include<vector>
using namespace std;
class Point {
private:
int id;
public:
Point(){}
void setId(int k){ id=k; }
int GetId() { return id; }
};
int main()
{
vector<Point> datasets;
for(int i=0; i< 5; ++i){
Point temp;
temp.setId(i);
datasets.push_back(temp);
}
for(int i=0;i<5;i++){
if (datasets[i].GetId() ==4){
return i;
}
}
}
Your program is probably working just fine, I think you're mixing up return with cout to actually print it out to the console, currently you're not printing anything and you're just returning i to the OS because you use return in main, making it a status code.
To see the output, use cout:
#include<iostream>
#include<vector>
using namespace std;
class Point{
private:
int id;
public:
Point(){}
void setId(int k){id=k;}
int GetId(){return id;}
};
int main()
{
vector<Point> datasets;
for(int i=0; i< 5; ++i){
Point temp;
temp.setId(i);
datasets.push_back(temp);
}
for(int i=0;i<5;i++){
if (datasets[i].GetId() ==4){
cout << "i is : " << i << endl;
break;
}
}
}
Edit to answer OP's comment:
use : vector<Point> datasets(5);
you arent printing any thing because you put return i after for loop
its certain that you wont got any result
#include<iostream>
#include<vector>
using namespace std;
class Point{
private:
int id;
public:
Point(){}
void setId(int k){id=k;}
int GetId(){return id;}
};
int main()
{
vector<Point> datasets;
for(int i=0; i< 5; ++i){
Point temp;
temp.setId(i);
datasets.push_back(temp);
}
for(int i=0;i<5;i++){
if (datasets[i].GetId() ==4){
cout<<i<<endl;
}
}
system("pause");
return 0;
}
you can use : datasets.insert(datasets.begin()+i,temp);
instead of datasets.push_back(temp);
for more flexibility in adding elements at the index i in vector class

Overloading operator =, error in reading string

I am trying to write an overload to the = operator so that it allows you directly assign one student object to another student object. So it will copy all the private data members. Here is what I have so far.
.h
#ifndef PROJECT3HEADER_H
#define PROJECT3HEADER_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student
{
public:
Student();
void Setlname(string lname);
void Setfname(string fname);
void SetAverage(float Average);
void SetLettergrade(char lettergrade);
void SetTestScore1(float score1);
void SetTestScore2(float score2);
void SetTestScore3(float score3);
void SetTestScore4(float score4);
void SetTestScore5(float score5);
string Getlname()const;
string Getfname()const;
float GetAverage()const;
char GetLetterGrade()const;
float GetScore1()const;
float GetScore2()const;
float GetScore3()const;
float GetScore4()const;
float GetScore5()const;
//void operator = (const Student & rhs);
Student operator=(const Student &rhs);
private:
string lname,fname;
float testScore[5];
float Average;
char lettergrade;
};
ostream & operator << (ostream &, const Student & pt);
//istream& operator >> (istream& in, Student& pt);
#endif
Studentmem.cpp
#include "Project3Header.h"
#include <iostream>
#include <string>
using namespace std;
Student::Student()
{
lname="";
fname="";
Average=0;
lettergrade=' ';
testScore[0]=0,testScore[1]=0,testScore[2]=0,testScore[3]=0,testScore[4]=0;
}
void Student::Setlname(string lname1){
lname=lname1;
}
void Student::Setfname(string fname1){
fname=fname1;
}
void Student::SetAverage(float average1){
Average=average1;
}
void Student::SetLettergrade(char lettergrade1){
lettergrade=lettergrade1;
}
void Student::SetTestScore1(float score1){
testScore[0]=score1;
}
void Student::SetTestScore2(float score2){
testScore[1]=score2;
}
void Student::SetTestScore3(float score3){
testScore[2]=score3;
}
void Student::SetTestScore4(float score4){
testScore[3]=score4;
}
void Student::SetTestScore5(float score5){
testScore[4]=score5;
}
string Student::Getlname()const {
return lname;
}
string Student::Getfname()const {
return fname;
}
float Student::GetAverage() const{
return Average;
}
char Student::GetLetterGrade()const{
return lettergrade;
}
float Student::GetScore1() const{
return testScore[0];
}
float Student::GetScore2() const{
return testScore[1];
}
float Student::GetScore3() const{
return testScore[2];
}
float Student::GetScore4() const{
return testScore[3];
}
float Student::GetScore5() const{
return testScore[4];
}
Student Student::operator=(const Student &rhs){
lname = rhs.lname;
fname = rhs.fname;
for (int i = 0; i < 5; i++){
testScore[i] = rhs.testScore[i];
}
return *this;
}
std::ostream& operator<<(std::ostream& out, Student const& obj)
{
out << "Lname: " << obj.Getlname() << "\n";
out << "fname: " << obj.Getfname() << "\n";
out << "Average: " << obj.GetAverage() << "\n";
out << "Grade: " << obj.GetLetterGrade() << "\n";
return out;
}
But when I try to use it in the main I get an error... error reading characters of string.
here is a little of my main
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Project3Header.h"
using namespace std;
//Tyler Smith
void StdInfo(Student array[], int size);
Student * MakeStudentArray(int size);
int main(){
ifstream inData;
int size = 0;
int highsize = 0;
char data[65535];
inData.open("F:\\grade.dat");
if (!inData)
{
cout << "Error opening file.\n";
cout << "Perhaps the file is not where indicated.\n";
return 1;
}
while (inData.getline(data, 65535)) {
size++;
}
inData.close();
cout << size;
Student s1;
Student * ptArr;
ptArr = MakeStudentArray(size);
for (int i = 0; i < size; i++){
ptArr[i]= s1;
}
StdInfo(ptArr,size);
/* for (int i = 0; i < size; i++){
cout << ptArr[i].Getlname() << ptArr[i].Getfname(); //<< ptArr[i].Get() << endl;
}*/
cout << ptArr[2].Getfname();
return 0;
}
Student * MakeStudentArray(int size)
{
return new Student[size];
}
void StdInfo(Student array[], int size){
ifstream in;
in.open("F:\\grade.dat");
string fname1,lname1="";
int Score1, Score2, Score3, Score4, Score5=0;
for (int i = 0; i < size; i++) {
in >> lname1;
in >> fname1;
in >> Score1;
in >> Score2;
in >> Score3;
in >> Score4;
in >> Score5;
array[i].Setlname(lname1);
array[i].Setfname(fname1);
array[i].SetTestScore1(Score1);
array[i].SetTestScore2(Score2);
array[i].SetTestScore3(Score3);
array[i].SetTestScore4(Score4);
array[i].SetTestScore5(Score5);
array[i] = array[i + 1];
//for (int j = 0; j < 5; j++) {
//in >> array[i].testScore[j];
//}
}
}
When I try it simply will not work. When I do it in debug mode it stops at the overloading operator and stays "rhs error reading characters of string"
The essense of your problem is not the (unconventional) assignment operator, but the MakeStudentArray function.
I hardcoded the size to 10, rather than reading it from a file, and used
Student * MakeStudentArray(int size)
{
return new Student[size];
}
taking care to call delete [] ptArr; after I was done in main (worrying about exceptions as I did) and all was well.
Your version allocates Students (maybe all over the heap), rather than an array.
If I change the question code you posted in main to
for (int i = 0; i < size; i++){
ptArr[i].operator= (s1);
//^------- you said 0, right?
}
then I get a problem. This is because the for loop assumes the "array" is contiguous, but your function newed an array of points, not an array of Students, so it walks into goodness only knows where when it tries to loop over the "array".
Using my suggestion version should solve the problem, since it allocates an array using new[size] and also makes it easier to delete.
Edit 1:
With the rest on main now posted, perhaps you do keep trying to set ptArr[0] several times in the loop, but then calling StdInfowill try to walk an "array" and will give you the error. If you wish to index into an array, using [] it must be contiguous, so the allocation function, MakeStudentInfo will still be the cause of the problem.
Edit2:
Now there is even more code, look at your StdInfo function. Without the extra details it does this:
for (int i = 0; i < size; i++) {
//...
array[i] = array[i + 1];
}
What are you trying to achieve here? It looks like set the current array[i] to something we haven't got to yet. Once i gets to size i+1 will be off the end of your array.
Removing that line would be a good idea.
The assignment operator should return a reference to the assigned object, not a copy. Also think about using the copy & swap idiom
Student& Student::operator=(const Student &rhs){