Compiler error while calling template functions - c++

I'm getting the following error upon compiling:
Compilation started at Wed Oct 5 03:05:32
|make -k proj1
|g++ proj1.cc -o proj1
|proj1.cc: In function ‘int main()’:
|proj1.cc:75:13: error: no matching function for call to ‘getData()’
|proj1.cc:75:13: note: candidate is:
|proj1.cc:46:6: note: template<class T> void getData(Vector<T>&, int&)
|proj1.cc:80:16: error: no matching function for call to
‘computeSum()’
|proj1.cc:80:16: note: candidate is:
|proj1.cc:28:6: note: template<class T> void computeSum(Vector<T>,
int, T&, bool&)
|proj1.cc:83:9: error: ‘success’ was not declared in this scope
|proj1.cc:84:27: error: ‘total’ was not declared in this scope
make: *** [proj1] Error 1
Compilation exited abnormally with code 2 at Wed Oct 5 03:05:33
Am I simply not calling my template
#include <std_lib_facilities.h>
#include <vector>
#include <string>
#include <iostream>
class T
{
public:
void computeSum(vector<T> in, int n, T &out, bool &success);
void getData(vector<T> &data, int &howMany);
};
template <class T>
// void computeSum(vector<T> data, int howMany, T& out, bool& success)
void computeSum(vector<T> data, int n, T &out, bool &success)
{
if (n < data.size()){
success = true;
int i = 0;
while (i<n){
out = out + data[i];
++i;
}
} else {
success = false;
cerr << "You can not request to sum up more numbers than there are.\n";
}
}
template <class T>
void getData(vector<T> &data, int &howMany)
{
cout << "Please insert the data:\n";
T n;
do{
cin >> n;
data.push_back(n);
} while (n<howMany);
cout << "This vector has " << data.size() << " numbers.\n";
}
void offerHelp()
{
cout << "Do you want help? ";
string help;
cin >> help;
if (help == "n" || help == "no"){
cout << endl;
}else{
cout << "Enter your data. Negative numbers will be added as 0. Ctrl-D to finish inputing values.\n";
}
}
int main()
{
offerHelp();
getData();
cout << "How many numbers would you like to sum?";
int howMany;
cin >> howMany;
computeSum();
if (success = true) {
cout << "The sum is " << total << endl;
} else {
cerr << "Oops, an error has occured.\n";
}
cout << endl;
return 0;
}

Your function is declared and defined as:
void getData(Vector&, int&)
You are calling it as:
getData();
Clearly, the compiler cannot find the function which takes no parameteres and hence the no mathching function error.
Same is the case for computeSum().
There are a host of other errors as well like success and total are two variables which are being accessed in main but not declared anywhere inside the main.

You are forgetting to pass the parameters to your functions e.g., :
void computeSum(vector<T> data, int n, T &out, bool &success)
computeSum();
Apparently the function signatures do not match. Also your class T is not declared as a template class. I think this is what you originally intended. The functions computeSum and getData do not implement the public member functions of the class.

Related

no matching conversion for functional-style cast from 'int' to 'ItemType'

I have a homework assignment in which I have to code some methods for a linked list and test with a driver from the professor. I keep running into this error:
no matching conversion for functional-style cast from 'int' to 'ItemType'
Here are my files for my "Node" class ItemType:
// ItemType.h.
#include <fstream>
const int MAX_ITEMS = 5;
enum RelationType {LESS, GREATER, EQUAL};
class ItemType{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print(std::ostream&) const;
void Initialize(int number);
private: int value;
};
And ItemType.cpp
#include <fstream>
#include <iostream>
#include "ItemType.h"
ItemType::ItemType()
{
value = 0;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::Initialize(int number)
{
value = number;
}
void ItemType::Print(std::ostream& out) const
// pre: out has been opened.
// post: value has been sent to the stream out.
{
out << value;
}
When I try to use the professors driver, I get an error with initializing the ItemType class with the constructor. I initialize them like so: classList.putItem(ItemType(4))
But I end up with the error stated above, I'm not sure where Im wrong, here is my driver:
#include "unsorted.h"
using namespace std;
int main() {
UnsortedType classList;
classList.PutItem(ItemType(4));
classList.PutItem(ItemType(5));
classList.PutItem(ItemType(4));
classList.PutItem(ItemType(4));
classList.PutItem(ItemType(8));
cout << "(original) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
classList.ShiftRight();
cout << "(shifted right) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
classList.DeleteItem(ItemType(4));
cout << "(delete all 4s) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
classList.ShiftRight();
cout << "(shift right) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
return 0;
}
You don't have a constructor for ItemType that takes an int. A simple fix would be to define that constructor:
ItemType(int v) : value{v} { }

Passing a string vector to a function and function prototype issue c++

In this example, the compiler says the function "list" doesn't have a definition, despite me writing one below. If I move the function definition to the top so there is no prototype, it compiles fine.
Can someone explain what's happening here?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void stuff();
void list(vector<string> things);
bool alive = true;
int main()
{
vector<string> things;
things.push_back("Lots");
things.push_back("Of");
things.push_back("Things");
do
{
cout << "What do you want to do?\n\n" << endl;
string input;
cin >> input;
if (input == "stuff")
{
stuff();
}
if (input == "list")
{
list();
}
} while (alive);
return 0;
}
void list()
{
cout << "The things are:\n\n";
for (int i = 0; i < things.size(); ++i)
{
cout << things[i] << endl;
}
}
void stuff()
{
cout << "Some stuff" << endl;
}
Your list function definition signature differs from your function declaration. The function signature should be the same. Your function definition signature should also accept one parameter:
void list(std::vector<string> things)
{
std::cout << "The things are:\n\n";
for (int i = 0; i < things.size(); ++i)
{
std::cout << things[i] << '\n';
}
}
And in your program you call the function with:
list();
where it should be:
list(things);
void list(vector<string> things); is not the same as void list(). You need to actually define your function as void list(vector<string> things) not just the prototype.

State design pattern - Compilation error

I get three errors while i try to compile this program.
I am expecting the following output
OFF-ctor Enter 0/1: 0 already OFF Enter 0/1: 1
going from OFF to ON ON-ctor dtor-OFF Enter 0/1: 1
already ON Enter 0/1: 0 going from ON to OFF OFF-ctor
dtor-ON Enter 0/1: 1 going from OFF to ON ON-ctor
dtor-OFF Enter 0/1: 0 going from ON to OFF OFF-ctor
dtor-ON Enter 0/1: 0 already OFF Enter 0/1:
Following is the program
#include <iostream>
using namespace std;
class Machine
{
class State *current;
public:
Machine();
void setCurrent(State *s)
{
current = s;
}
void on();
void off();
};
class State
{
public:
virtual void on(Machine *m)
{
cout << " already ON\n";
}
virtual void off(Machine *m)
{
cout << " already OFF\n";
}
};
void Machine::on()
{
current->on(this);
}
void Machine::off()
{
current->off(this);
}
class ON: public State
{
public:
ON()
{
cout << " ON-ctor ";
};
~ON()
{
cout << " dtor-ON\n";
};
void off(Machine *m);
};
class OFF: public State
{
public:
OFF()
{
cout << " OFF-ctor ";
};
~OFF()
{
cout << " dtor-OFF\n";
};
void on(Machine *m)
{
cout << " going from OFF to ON";
m->setCurrent(new ON());
delete this;
}
};
void ON::off(Machine *m)
{
cout << " going from ON to OFF";
m->setCurrent(new OFF());
delete this;
}
Machine::Machine()
{
current = new OFF();
cout << '\n';
}
int main()
{
void(Machine:: *ptrs[])() =
{
Machine::off, Machine::on//Error2->invalid use of non-static member function 'void Machine::off()'
//Error3->invalid use of non-static member function 'void Machine::on()'
};
Machine fsm;
int num;
while (1)
{
cout << "Enter 0/1: ";
cin >> num;
(fsm. *ptrs[num])(); //Error1->expected unqualified-id before '*' token
}
}
The code was taken from sourcemaking.com under state design pattern.
I ran it in eclipse and linux g++.
Kindly help.
To get a pointer to a member function, you need to use an & (even though it's optional for getting a pointer to a non-member function): &Machine::off, &Machine::on
For the other, you need to realize that .* is a single token, so you need to remove the space between the two characters: (fsm.*ptrs[num])();
void (Machine::*ptrs[])() =
{
&Machine::off, // note: explicit &
&Machine::on
};
Machine fsm;
int num;
while (1)
{
cout << "Enter 0/1: ";
cin >> num;
(fsm.*ptrs[num])(); // note: no space between . and *
}
That still leaves the following warnings:
try.cc:19:22: warning: unused parameter 'm' [-Wunused-parameter]
try.cc:23:22: warning: unused parameter 'm' [-Wunused-parameter]
try.cc: In member function 'virtual void OFF::on(Machine*)':
try.cc:68:20: warning: deleting object of polymorphic class type 'OFF' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
try.cc: In member function 'virtual void ON::off(Machine*)':
try.cc:76:14: warning: deleting object of polymorphic class type 'ON' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]

Declare a pointer to different class in member funcion

#include<iostream>
using namespace std;
class StudentClass{
friend class Oopclass;
public:
StudentClass();
void setStudentData();
void printStudentScore();
StudentClass* next;
~StudentClass();
private:
string std_ID;
int sID, sMid, sFin, sOP, hw[3];
double sTotal;
};
class OopClass{
friend class StudentClass;
public:
OopClass();
void setOopData();
void printOopScore();
void queryOopScore();
void findOopAverageScore();
void addStudentData();
void deleteStudentData();
void updateStudentData();
~OopClass();
private:
string professor, subject;
int cSn;
double cSAvg;
StudentClass* cSAddr;
};
int main(int argc, char **argv){
OopClass Info[3];
Info[0].setOopData();
return 0;
}
StudentClass::StudentClass(){
}
void StudentClass(){
}
void StudentClass::setStudentData() {
}
StudentClass::~StudentClass(){
}
OopClass::OopClass(){
}
void OopClass::setOopData() {
cout << "Professor?" << endl;
cin >> professor;
cout << "Subject?" << endl;
cin >> subject;
cout << "How many students do you want to input ?" << endl;
int number = 0;
cin >> number;
cSAddr = new StudentClass;
//for(int i = 0; i < number; i ++) setStudentData();
}
OopClass::~OopClass(){
}
When I tired to write "cSAddr = new StudentClass;" ,it come up to
error about
" C:\Users\lypan\Documents\final.cpp In member function 'void
OopClass::setOopData()':
63 14 C:\Users\lypan\Documents\final.cpp [Error] expected
type-specifier before 'StudentClass'
63 14 C:\Users\lypan\Documents\final.cpp [Error] invalid conversion
from 'int*' to 'int' [-fpermissive]
63 14 C:\Users\lypan\Documents\final.cpp [Error] expected ',' or ';'
before 'StudentClass' "
And I have no idea why I do wrong, please tell me what I make mistake. Thx!
The only error here is that you didn't implement the constructors for your classes. But that should yield a linker error.
This probably isn't your code, but it might be that you don't include StudentClass.h and OopClass.h (or whatever) in the file where you define OopClass::setOopData().
EDIT:
void StudentClass()
you declare a function called StudentClass on line 46 - http://codepad.org/9DP6zpHU - you probably forgot to qualify it.
StudentClass::StudentClass(){
}
//THIS!
void StudentClass(){
}
void StudentClass::setStudentData() {
}
The error is you define a function with the same name as the class StudentClass:
void StudentClass(){
}
Remove that function and it should work better.

Multidimensional Array trouble (bounds)

here is the code with the problem:
#ifndef WEAPONS_H_INCLUDED
#define WEAPONS_H_INCLUDED
#include "Battleship header.h"
void Player::torpedo(string enemyEvtTracker[][10], string myhitboard[][10])
{
string bcn; //board column number
cout << "Enter board column number (1-9): "; cin >> bcn; flush();
if(bcn!="1"&&bcn!="2"&&bcn!="3"&&bcn!="4"&&bcn!="5"&&bcn!="6"&&bcn!="7"&&bcn!="8"&&bcn!="9")
{cout <<endl<< "That is not a valid number.";}
return;
}
#endif // WEAPONS_H_INCLUDED
here is the class Player:
#ifndef BATTLESHIPPLAYERCLASS_H_INCLUDED
#define BATTLESHIPPLAYERCLASS_H_INCLUDED
using namespace std;
class Player // define a class for a player
{
void torpedo(string enemyEvtTracker[10][10], string myhitboard[10][10]);
void cannon();
void scatter();
void menu();
friend int main(int, char*[]); //Let main access the protected members
friend int routine_END(void);
public:
void displaydata()
{cout << money << endl << gunpowder << endl << ammo_cannon << endl << ammo_4hit << endl;}
string savename;
int gunpowder;
int ammo_cannon;
int ammo_4hit; string gun_4;
int ammo_scatter; string gun_s;
int money;
Player(string name){money=18000; gunpowder=100;ammo_cannon=20; ammo_4hit=0; ammo_scatter=0; gun_4="OFF"; gun_s="OFF";playername=name;} //Define the constructor
void simplegame(void) {gunpowder=99999999; ammo_cannon=999999999; ammo_scatter=4; gun_s="ON";}
void getname(string *playername, int option)
{
if (option==1)
{cout << "Enter your name here player 1:"; cin >> *playername;}
else {cout << "Enter your name here player 2:"; cin >> *playername;}
}
string playername;
char mainRowGuess;
int check_transaction (int mymoney, int moneyowed)
{
if (mymoney-moneyowed<<0) {return 0;}
else {return 1;}
}
void change_Answer_to_number(char row,int* outputRow)
{
if (row=='A'||row=='a'){*outputRow =1;}
else if (row=='B'||row=='b'){*outputRow =2;}
else if (row=='C'||row=='c'){*outputRow =3;}
else if (row=='D'||row=='d'){*outputRow =4;}
else if (row=='E'||row=='e'){*outputRow =5;}
else if (row=='F'||row=='f'){*outputRow =6;}
else if (row=='G'||row=='g'){*outputRow =7;}
else if (row=='H'||row=='h'){*outputRow =8;}
else if (row=='I'||row=='i'){*outputRow =9;}
else {*outputRow = 0;}
}
void changeCharToNumber(char row, int* outputRow)
{
if (row=='1'){*outputRow=1;}
else if (row=='2'){*outputRow=2;}
else if (row=='3'){*outputRow=3;}
else if (row=='4'){*outputRow=4;}
else if (row=='5'){*outputRow=5;}
else if (row=='6'){*outputRow=6;}
else if (row=='7'){*outputRow=7;}
else if (row=='8'){*outputRow=8;}
else if (row=='9'){*outputRow=9;}
else {cout << "Unexpected Error." << endl; *outputRow=0;}
}
char airRowStart; char airColStart; char aircraftDirec;
char destRowStart; char destColStart; char destroyerDirec;
char subRowStart; char subColStart; char subDirec;
char patrolStart; char patrolDirec;
/// START MENU FUNCTION
void error_money(void) {cout << "Not enough money!";}
char startRowAircraftCarrier;
char startRowDestroyer;
char startRowSub;
char startRowPatrolBoat;
friend int routine_END (void);
friend void menu (int* gunpowder, int* ammo_cannon, int* ammo_4hit, int* ammo_scatter, int* money, string* gun_4, string* gun_s);
};
#endif // BATTLESHIPPLAYERCLASS_H_INCLUDED
and this generates the following build log...
-------------- Build: Debug in Advanced Battleship Obj
[ 50.0%] Compiling: main.cpp
C:\Advanced_Battleship_Revised_5111\main.cpp:32:
warning: ignoring #pragma comment In
file included from
C:\Advanced_Battleship_Revised_5111\/imputoutput.h:9,
from C:\Advanced_Battleship_Revised_5111\/Battleship
header.h:3,
from C:\Advanced_Battleship_Revised_5111\main.cpp:25:
C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:74:
warning: 'int routine_END()' is
already a friend of class 'Player'
C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:
In member function 'int
Player::check_transaction(int, int)':
C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:33:
warning: suggest parentheses around
'-' inside '<<' In file included from
C:\Advanced_Battleship_Revised_5111\main.cpp:27:
C:\Advanced_Battleship_Revised_5111\/BattleshipMenu.h:
In member function 'void
Player::menu()':
C:\Advanced_Battleship_Revised_5111\/BattleshipMenu.h:118:
warning: label 'GUNPOWDER_MENU_1'
defined but not used
C:\Advanced_Battleship_Revised_5111\/BattleshipMenu.h:166:
warning: label
'CIN_WEAPON_OPTION_SCATTER_CANNON'
defined but not used In file included
from
C:\Advanced_Battleship_Revised_5111\main.cpp:30:
C:\Advanced_Battleship_Revised_5111\/weapons.h:
At global scope:
> C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: declaration of
'enemyEvtTracker' as multidimensional
array must have bounds for all
dimensions except the first
C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: expected ')' before ',' token
C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: expected constructor,
destructor, or type conversion before
',' token
C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: expected constructor,
destructor, or type conversion before
'myhitboard' Process terminated with
status 1 (0 minutes, 1 seconds) 4
errors, 5 warnings
Player::torpedo(string enemyEvtTracker[10][10], string myhitboard[10][10])
{
//..
}
This is the definition of the member function. Where is the declaration?
My guess is that in the declaration you've not mentioned the size of the array, as you did in the definition. It seems you've written simply enemyEvtTracker [][]? See the definition of Player class, and verify how you've declared torpedo member function in it.