C++ class instance declaration no matching function - c++

I'm practicing OOP in C++ and I'm creating a bank account class. Here is the class definition:
#include <iostream>
#include <cmath>
#include <limits>
#include <string>
#include <cstring>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
using namespace std;
const int acc_num_length = 8;
const int acc_id_num_length = 13;
class bankAccount {
private:
int acc_num; // account number
int acc_id_num; // account holder ID number
string acc_tel_no; // account holder telephone number
string acc_first_name; // account holder first name
string acc_last_name; // account holder last name
public:
/* constructor */
bankAccount() {
acc_num = 0;
acc_id_num = 0;
acc_tel_no = "";
acc_first_name = "";
acc_last_name = "";
}
/* detail retrieval */
int get_acc_num() {
return(acc_num);
}
int get_acc_id_num() {
return(acc_id_num);
}
string get_acc_tel_no() {
return(acc_tel_no);
}
string get_acc_first_name() {
return(acc_first_name);
}
string get_acc_last_name() {
return(acc_last_name);
}
};
Now in my main program I'm trying to declare an instance of this class,
int main() {
float acc_num = 0;
while ( ((cout << "Enter account number: ") && !(cin >> acc_num)) || (floor(acc_num) < acc_num) ) {
cout << "Invalid account number.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
int acc_num_int = acc_num;
string acc_num_string = to_string(acc_num_int);
while ( acc_num_string.length() != acc_num_length ) {
cout << "Invalid account number (8 characters only).\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> acc_num;
cout << "Enter account number: \n";
}
bankAccount ba1 = bankAccount(acc_num_int,0, "","","");
bankAccount ba2 = bankAccount();
return 0;
}
Now everything works except my instance declarations,
C:/Cartrack/C++/Creating a form/Form/main.cpp: In function 'int
main()': C:/Cartrack/C++/Creating a form/Form/main.cpp:68:67: error:
no matching function for call to 'bankAccount::bankAccount(int&, long
long int, const char [1], const char [1], const char [1])'
bankAccount ba1 = bankAccount(acc_num_int,0, "","","");
^ C:/Cartrack/C++/Creating a form/Form/main.cpp:25:2: note: candidate:
bankAccount::bankAccount() bankAccount() { ^~~~~~~~~~~
C:/Cartrack/C++/Creating a form/Form/main.cpp:25:2: note: candidate
expects 0 arguments, 5 provided C:/Cartrack/C++/Creating a
form/Form/main.cpp:16:7: note: candidate:
bankAccount::bankAccount(const bankAccount&) class bankAccount {
^~~~~~~~~~~ C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate expects 1 argument, 5 provided
C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate:
bankAccount::bankAccount(bankAccount&&) C:/Cartrack/C++/Creating a
form/Form/main.cpp:16:7: note: candidate expects 1 argument, 5
provided mingw32-make.exe[1]: * [Form.mk:97: Debug/main.cpp.o] Error
1 mingw32-make.exe[1]: Leaving directory 'C:/Cartrack/C++/Creating a
form/Form' mingw32-make.exe: * [Makefile:5: All] Error 2
====1 errors, 6 warnings====
I'm not sure why there's no matching call. I'm passing it two ints and three strings, which seems to be the mistake most people make when I've read through the existing threats.
Can anyone help please?
Thanks!
Zane

You need to declare parameterized constructors as per your need. All you have is default(or zero-argument) constructor.
Please read the constructor section of any C++ book/tutorial.
Moreover, you should not create objects like
bankAccount ba1 = bankAccount(acc_num_int,0, "","","");
Above statement will create a nameless object and do copy construction of ba1. Please read about copy constructors. When you need a single object this is an overload for extra function call. Instead you can create your object like below
bankAccount ba1(acc_num_int,0, "","","");

Related

How to Define CountProbation() function using Functions from other Files?

I have been racking my brain on how to properly define the function CountProbation() properly in the Course.css file. I know that a for and if loop should probably be included but I am having trouble including functions from other files, even though I am including a header tag at the beginning of the current Course.css file.
Below are the C++ files that are given in the lab:
NOTE: Only the Course.cpp file is the one that needs to be edited. All of the other files are READ ONLY and purely for your information as the reader.
Sorry if it seems like a lot of code, but I didn't just want to include the Course.cpp file because then you might not understand the program.
Here are the compiler Errors/Warnings:
Course.cpp: In member function ‘int Course::CountProbation()’:
Course.cpp:8:18: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<Student>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
8 | for (int i=0; i < roster.size(); ++i) {
| ~~^~~~~~~~~~~~~~~
Course.cpp:9:9: error: ‘GetGPA’ was not declared in this scope
9 | if (GetGPA() < 2.0) {
| ^~~~~~
Course.cpp
#include <iostream>
#include "Course.h"
#include "Student.h"
using namespace std;
int Course::CountProbation() { //This is the function that I have tried to define as you can see by my
int probCount; //code
for (int i=1; i < roster.size(); ++i) {
if (GetGPA() < 2.0) {
probCount = probCount + 1;
}
}
return probCount;
}
void Course::AddStudent(Student s) {
roster.push_back(s);
}
Course.h (READ ONLY)
#ifndef COURSE_H
#define COURSE_H
#include <vector>
#include "Student.h"
class Course {
public:
int CountProbation();
void AddStudent(Student s);
private:
vector<Student> roster; //collection of Student objects
};
#endif
Main.cpp (READ ONLY)
#include <iostream>
#include <string>
#include "Course.h"
using namespace std;
int main() {
Course course;
int probCount;
// Example students for testing
course.AddStudent(Student("Henry", "Cabot", 3.2));
course.AddStudent(Student("Brenda", "Stern", 1.1));
course.AddStudent(Student("Lynda", "Robison", 2.4));
course.AddStudent(Student("Jane", "Flynn", 1.8));
probCount = course.CountProbation();
cout << "Probation count: " << probCount << endl;
return 0;
}
Student.h (READ ONLY)
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
// Class representing a student
class Student {
public:
Student(string first, string last, double gpa);
double GetGPA() ;
string GetLast();
private:
string first; // first name
string last; // last name
double gpa; // grade point average
};
#endif
Student.cpp (READ ONLY)
#include "Student.h"
// Student class constructor
Student::Student(string first, string last, double gpa) {
this->first = first; // first name
this->last = last; // last name
this->gpa = gpa; // grade point average
}
double Student::GetGPA() {
return gpa;
}
string Student::GetLast() {
return last;
}
This loop (as it was coded in the original question, before it was edited):
for (int i=1; i < roster.size(); ++i) {
if (Student.GetGPA() < 2.0) {
probCount = probCount + 1;
}
}
is flawed for three reasons:
i should start from 0, rather than 1
i should be of type size_t, rather than int
Student is a type, not a variable
A nice way to solve all these problems is to use a ranged for loop, like this:
for (Student &student : roster)
{
if (student.GetGPA() < 2.0)
probCount = probCount + 1;
}
As mentioned in the comments, you also need to initialise probCount to zero before entering the loop:
int probCount = 0;
Finally, it's worth noting that the above loop would normally be coded as:
for (const Student &student : roster)
{
...
but that won't work here as GetGPA is not declared as a const method. Thanks to #user4581301 for pointing this out.

Using member function to solve equation C++

I am currently trying to create a program to calculate the mass of a rocket with given time values by passing an array to a member function of a class. I get these two errors and can't seem to figure out how to get rid of them. Any suggestions are much appreciated, thank you.
23 8 [Error] prototype for 'double equip::calcmass(double)' does not match any in class 'equip'
13 10 [Error] candidate is: double equip::calcmass()
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstring>
using namespace std;
class equip
{
public:
double mass[999999999], velocity, height, time[999999999];
double calcmass();
private:
double T = 7000;
double g = 32.2;
double K = 0.008;
};
double equip::calcmass(double time)
{
int i = 0;
for(i=0; i=999999999; i++)
{
return mass[i] = (3000 - 40 * time[i]) / g;
}
}
int main()
{
int i = 0;
equip rocket;
ifstream infile;
string filename;
cout<<"Enter input file name for time (time): ";
cin>>filename;
infile.open(filename.c_str());
while(infile.fail())
{
cerr<<"Error opening file. \n";
cout<<"Enter file name: ";
cin>>filename;
infile.open(filename.c_str());
}
for(i=0; i<999999999; i++)
{
infile>>rocket.time[i];
}
for(i=0; i<999999999; i++)
{
cout<<rocket.mass[i];
}
return 0;
}
In your class definition you've declared
double calcmass()
In the definition of the member function it's
double calcmass(double time)
They do not match. One takes a double as argument and the other does not.
You were missing a bunch of headers, and in the function declaration was missing the parameter presents in the function definition:
#include <cmath>
#include <cstring>
#include <fstream>
#include<iostream>
using namespace std;
class equip
{
public:
...
double calcmass(double time); // here was missing the parameter
...
};
double equip::calcmass(double time)
{
...
}
Also you are doing time[i] on time which is a double, so you can't use operator[]... If you want to pass an array, you can use pointer to double:
class equip{
...
double calcmass(double* time)
}
double equip::calcmass(double* time)
{
...
}

Passing vector to member function of class

I am trying to pass a vector to a member function, but keep coming up with these errors:
27 28 [Error] 'double equip::calcmass' is not a static member of 'class equip'
13 19 [Error] invalid use of non-static data member 'equip::time'
27 24 [Error] from this location
28 1 [Error] expected unqualified-id before '{' token
How can I correct this?
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
class equip
{
public:
vector <double> time;
vector <double> mass;
vector <double> velocity;
vector <double> height;
double calcmass();
double calcvelocity();
double calcheight();
double calctmax();
private:
double T = 7000;
double g = 32.2;
double K = 0.008;
};
double equip::calcmass(time);
{
int i = 0;
for(i=0; i<time.size(); i++)
{
return mass[i] = (3000 - 40 * time[i]) / g;
}
}
int main()
{
int i = 0;
ifstream infile;
string filename;
cout<<"Enter input file name for time (t): ";
cin>>filename;
infile.open(filename.c_str());
while(infile.fail())
{
cerr<<"Error opening file. \n";
cout<<"Enter file name: ";
cin>>filename;
infile.open(filename.c_str());
}
for(i=0; i<time.size(); i++)
{
infile>>time[i];
}
Your code is not working because you defined a a class equip, but never created an instance of it in main, and then you try to read a file content into a member of class time. Also, your function calcmass in the class definition has no arguments, but later you declare it with argument of undetermined type time. Remove the argument of a function, it will see time anyway as they are both members of the same class.

Error no match for 'operator=' (operand types are 'Person' and 'Person*')

In function 'int main()':
41 14 [Error] no match for 'operator=' (operand types are 'Person' and 'Person*')
41 14 [Note] candidate is:
8 8 [Note] Person& Person::operator=(const Person&)
8 8 [Note] no known conversion for argument 1 from 'Person*' to 'const Person&'
28 recipe for target 'main112.o' failed
Homework:
Write a program for processing passenger information. Information includes:
1) Full name of the passenger.
2) Flight number.
3) Luggage weight
The program should allow the user to:
1) Read data from the keyboard and display it.
2) Calculate the number of passengers with the weight of baggage which is more than 10 kg
#include <string>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
using namespace std;
struct Person
{
string name;
string race;
int weight;
void write();
void show();
void check();
};
void Person::show()
{
cout<<"ÔÈÎ: "<<name<<endl;
cout<<"Íîìåð ðåéñà: "<<race<<endl;
cout<<"Âåñ áàãàæà: "<<weight<<endl;
}
void Person::write()
{
cout<<"Ââåäèòå ÔÈÎ: ";
getline(cin,name);
cout<<"Ââåäèòå íîìåð ðåéñà: ";
getline(cin,race);
cout<<"Ââåäèòå âåñ áàãàæà: ";
cin>>weight;
}
int main()
{
Person* persons=new Person[4];
for (int i = 0; i < 4; i++)
{
persons[i] = new Person();
persons[i].write();
}
for (int i = 0; i < 4; i++)
{
persons[i].show();
}
cout<<"Ñ áàãàæîì áîëüøå 10 êã: ";//<<counter<<" ÷åëîâåê"<<endl;
return 0;
}
When you call this line:
Person* persons = new Person[4];
Then this already makes four persons. You don't need the following line:
persons[i] = new Person();
Just remove this line. It returns a Person* which can't be assigned to persons[i], which refers to an actual Person (and not a pointer). But since you already made the four Persons in the first line, you don't need this line at all.
You should, however, delete the persons when you're done:
delete[] persons;

Too many arguments to function

I am getting this error from my header file: too many arguments to function void printCandidateReport();. I am fairly new to C++ and just need some guidance in the right direction to solving this error.
My header file looks like this:
#ifndef CANDIDATE_H_INCLUDED
#define CANDIDATE_H_INCLUDED
// Max # of candidates permitted by this program
const int maxCandidates = 10;
// How many candidates in the national election?
int nCandidates;
// How many candidates in the primary for the state being processed
int nCandidatesInPrimary;
// Names of the candidates participating in this state's primary
extern std::string candidate[maxCandidates];
// Names of all candidates participating in the national election
std::string candidateNames[maxCandidates];
// How many votes wone by each candiate in this state's primary
int votesForCandidate[maxCandidates];
void readCandidates ();
void printCandidateReport ();
int findCandidate();
#endif
and the file calling this header file:
#include <iostream>
#include "candidate.h"
/**
* Find the candidate with the indicated name. Returns the array index
* for the candidate if found, nCandidates if it cannot be found.
*/
int findCandidate(std::string name) {
int result = nCandidates;
for (int i = 0; i < nCandidates && result == nCandidates; ++i)
if (candidateNames[i] == name)
result = i;
return result;
}
/**
* Print the report line for the indicated candidate
*/
void printCandidateReport(int candidateNum) {
int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
if (delegatesWon[candidateNum] >= requiredToWin)
cout << "* ";
else
cout << " ";
cout << delegatesWon[candidateNum] << " " << candidateNames[candidateNum]
<< endl;
}
/**
* read the list of candidate names, initializing their delegate counts to 0.
*/
void readCandidates() {
cin >> nCandidates;
string line;
getline(cin, line);
for (int i = 0; i < nCandidates; ++i) {
getline(cin, candidateNames[i]);
delegatesWon[i] = 0;
}
}
why am I getting this error and how can I fix it?
On the header file you declare:
void printCandidateReport ();
But on the implementation is:
void printCandidateReport(int candidateNum){...}
Change the header file to
void printCandidateReport(int candidateNum);
The error message is telling you precisely what the problem is.
In your header file you declare the function with no parameters:
void printCandidateReport ();
In the source file you define it with a parameter of type int:
void printCandidateReport(int candidateNum){
Either add the missing parameter to the declaration, or remove it from the definition.
The error too many arguments to function can be fixed by eliminating the excess arguments
(parameters) in the function .
This error occurred because your header file has no parameter values, and in the actual source code you use the int parameter.
You have two choices, you can add the missing int parameter in the function declaration, or remove it entirely from the function.
The header file declares the function printCandidateReport() with no parameters and the cpp file defines the function with an int parameter. Just add the int parameter to the function declaration in the header file to fix it