Dynamic Allocation. No idea what these errors mean - c++

This is a project in which I had to dynamically create an array of structs. No idea what these errors mean or what is wrong with my code.
Based on the advice given so far here most of my problems have been solved. Here is the short list of remaining errors.
/tmp/ccdjbURO.o: In function `main':
assignment8.cpp:(.text+0x5a): undefined reference to `getData(menuItemType&, int&, std::basic_ifstream<char, std::char_traits<char> >&)'
assignment8.cpp:(.text+0x116): undefined reference to `showMenu(menuItemType, int)'
assignment8.cpp:(.text+0x1a5): undefined reference to `showMenu(menuItemType, int)'
assignment8.cpp:(.text+0x29f): undefined reference to `makeSelection(int&, int, int)'
assignment8.cpp:(.text+0x2eb): undefined reference to `printCheck(menuItemType, int, int)'
collect2: error: ld returned 1 exit status
Here are my function prototypes and definitions as requested. I see no difference in the function signatures in the prototypes and definition headings vs the formatting of any function calls in the body of my program.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct menuItemType
{
string menuItem;
double menuPrice;
};
const double TAX = 0.05;
const string FILE_NAME = "Ch9_Ex5Data.txt";
int getData(menuItemType&, int&, ifstream);
int makeSelection(int&, int, int);
void showMenu(menuItemType, int);
void printCheck(menuItemType, int, int);
void getInt(int&);
void getChar(char&);
//********************************************************************************
//* getData
//********************************************************************************
int getData(menuItemType* &menuList, int& listSize, ifstream& inFile)
{
inFile.open("Ch9_Ex5Data.txt");
inFile >> listSize;
if (inFile.fail())
return -1;
menuList = new menuItemType[listSize];
for(int i = 0; i < listSize; i++)
{
getline(inFile, menuList[i].menuItem);
inFile >> menuList[i].menuPrice;
if (inFile.fail())
return -1;
break;
}
122,1 47%
return 1;
}
//********************************************************************************
//* makeSelection
//********************************************************************************
int makeSelection(int* &orderList, int quantity, int index)
{
if ((orderList[index] + quantity) < 0)
{
cout << "Quantity selected makes total number ordered less than 0"
<< endl << endl;
return 1;
}
else
{
orderList[index] = orderList[index] + 1;
return -1;
}
}
//********************************************************************************
//* showMenu
//********************************************************************************
void showMenu(menuItemType *menuList, int listSize)
{
cout << fixed << showpoint << setprecision(2) << endl << endl
<< "------Today's Menu------" << endl;
for(int i = 0; i < listSize; i++)
{
cout << left << setw(18) << menuList[i].menuItem << "$ "
<< right << setw(4) << menuList[i].menuPrice << endl;
}
cout << "------------------------"
<< endl << endl;
}
//********************************************************************************
//* printCheck
//********************************************************************************
void printCheck(menuItemType *menuList, int *orderList, int listSize)
{
int taxDue = 0;
int amntDue = 0;
cout << fixed << showpoint << setprecision(2) << endl << endl
<< "------Your Reciept------" << endl;
for (int i = 0; i < listSize; i++)
{
if (orderList[i] > 0)
{
cout << left << setw(2) << orderList[i] << " "
<< setw(15) << menuList[i].menuItem
<< right << setw(5) << (orderList[i] * menuList[i].menuPrice)
<< endl;
amntDue += (orderList[i] * menuList[i].menuPrice);
}
}
210,1 73%
taxDue = amntDue * TAX;
amntDue = amntDue * (1 + TAX);
cout << endl << right << setw(17) << "Tax: $ "
<< setw(7) << taxDue
<< endl << right << setw(17) << "Amount Due: $ "
<< setw(7) << amntDue
<< endl
<< "------------------------" << endl << endl;
}
187,0-1 64%

The errors do look unfriendly, but the issues are simple.
You pass an ifstream in
int getData(menuItemType* &menuList, int& listSize, ifstream inFile)
by value, which implicitly tries to make copy of it.
I think its not possible to copy istream and ostream. You have to pass them by reference.
Also, as #PeterT said in comment, there is no version of getline you are tryng to invoke. Both default versions expect input and string as arguments.

Your forward declarations ARE different from the definitions. Replace them with something like this:
int getData(menuItemType*&, int&, ifstream&);
int makeSelection(int*&, int, int);
void showMenu(menuItemType*, int);
void printCheck(menuItemType*, int*, int);
void getInt(int&);
void getChar(char&);

Related

Semantic error in the selection condition of the type of LED lamps

I have a class of light bulbs. There are methods and constructors in this class. There is even a destructor) The problem is that I have to determine and display information about class members with type "n" in the TEST() method (LED lamps).
To implement this task, he developed the gettype() method, which returns the type of an object, and, in fact, the TEST() method, which displays information about light bulbs.
The problem is that nothing works for me. I tried a lot of things, but it doesn’t work out for me to implement this task. I'm new to programming (
Code:
#include <iostream>
using namespace std;
class lamp
{
public:
// methods
void TEST(void);
char* gettype (void);
void INIT(void);
void SHOW(void);
// construcrors
lamp();
lamp(const char *t, int powe, const char *c, double cos);
lamp(const lamp & obj);
// destructor
~lamp();
private:
// data
char type[100]; // LED, energy-saving or incandescent lamp
int power; // LED lamp - "n"
char color[100];
double cost;
};
lamp::lamp() {
cout << "This object was created in the default constructor.\n";
strcpy(type, "");
power = 0;
strcpy(color, "");
cost = 0;
}
lamp::lamp(const char *t, int powe, const char *c, double cos) {
cout << "This object was created in the constructor with parameters.\n";
strcpy(type, t); //*t
power = powe;
strcpy(color, c); //*c
cost = cos;
}
lamp::lamp(const lamp & obj) {
cout << "This object was created in the copy constructor.\n";
strcpy(type, obj.type);
power = obj.power;
strcpy(color, obj.color);
cost = obj.cost;
}
lamp::~lamp() {
cout << "Deletion of object by destructor.\n";
}
void lamp::SHOW(void) {
cout << "Lamp Information:\n";
cout << "\nType > " << type;
cout << "\nPower > " << power;
cout << "\nColor > " << color;
cout << "\nCost > " << cost << endl;
}
void lamp::INIT(void) {
cout << "Enter lamp information:\n";
cout << "\nType (if LED, then n) > "; cin >> type;
cout << "\nPower > "; cin >> power;
cout << "\nColor > "; cin >> color;
cout << "\nCost > "; cin >> cost;
}
char* lamp::gettype (void) {
return type;
}
void lamp::TEST(void) {
cout << "\nType > " << type;
cout << "\nPower > " << power;
cout << "\nColor > " << color;
cout << "\nCost > " << cost << endl;
}
void main() {
setlocale(0, "");
// default constructor for 1 class instance
lamp l1;
cout << "Entering data for the first product." << endl;
l1.INIT();
// constructor with parameters for 2 class instances
cout << endl << "Information about the second object: \n";
lamp l2("n", 950, "yellow", 1580);
// copy constructor for the third object
cout << endl << "Information about the third object: \n";
lamp l3(l2);
// Derived information about all the lamps using the method SHOW
l1.SHOW();
l2.SHOW();
l3.SHOW();
// I create an array of two objects using the default constructor
lamp la[2];
I enter data into an array of objects using the method INIT
cout << "Fill an array of objects with 2 elements." << endl;
for(int i = 0; i < 2; i++) {
la[i].INIT();
}
// I output data from an array of objects using the method SHOW
cout << "Showing items." << endl;
for (int i = 0; i < 2; i++) {
la[i].SHOW();
}
// looking for and displaying information about LED lamps
cout << "Search and display information about LED lamps." << endl;
for (int i = 0; i < 3; i++) {
if (la[i].gettype() == "n") {
cout << endl << " lamp number : " << (i + 1) << endl;
la[i].TEST();
cout << endl;
}
}
system("pause");
}
There are several errors in your code:
strcpy is included in <cstring> which is missed. You need to add it in the beginning:
#include <cstring>
main() function should be declared as int main() and you need to add a return statement
int main() {
//YOUR CODE HERE
return 0;
}
You missed a comment sign at line 104
lamp la[2];
//I enter data into an array of objects using the method INIT
cout << "Fill an array of objects with 2 elements." << endl;
After fixed, your code should be able to run.

Visual Studio 2013 programming "<<" is not matching operands?

I am getting an error saying the operand "<<" (right before times3(x) in the main function ) does not match the operand types being outputted in that line. What am I doing wrong? I searched for errors similar to it and found that its an inclusion error but i thought having would fix it. Also, countdown(seconds) in the main function is not being recognized and giving me an error. Why is that? The problems keep occurring when working with void.
'
#include <iostream>
#include <string>
#include <cstdlib>
#include <limits>
using namespace std;
bool die(const string & msg);
double triple(double x);
double times9(double x);
void triple(double & result, double x);
void times3(double & x);
void countdown(unsigned seconds);
bool restore();
int main(){
double x;
cout << "x: " << endl;
cin >> x;
cout << "The triple of " << x << " is " << triple(x) << endl;
cout << "9 times of " << x << " is " << times9(x) << endl;
cout << "3 times of " << x << " is " << times3(x) << endl;
unsigned seconds;
cout << "seconds: " << endl;
cin >> seconds;
cout << countdown(seconds) << endl;
}
bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
double triple(double x){
return 3 * x;
}
double times9(double x){
return 3 * triple(x);
}
void triple(double & result, double x){
x = 3 * x;
}
void times3(double & x){
x = triple(x);
}
void countdown(unsigned & seconds){
unsigned count = seconds;
cin >> seconds || die("input failure");
for (unsigned i = seconds; i <= size; i--){
cout << i << endl;
}
cout << "Blast off! " << endl;
}
bool resotre(){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return cin.good();
}'
As mentioned in earlier answer, you need to change the return type of your function from void to the data type of variable your trying to print.
Another issue in your code is with function void countdown(unsigned & seconds)
Declaration and definition of the functions are different.
You have declared it as void countdown(unsigned seconds); but at the time of defining it you are using void countdown(unsigned & seconds). In declaration you are declaring it to take arguments by value but in definition you are making it to take arguments by reference.
Also in the for loop of the function countdown you have written
for (unsigned i = seconds; i <= 0; i--), this won't print any output, since your condition is i<=0, i think you tried to type i >= 0. :)
times3 returns void. Try:
times3(x);
cout << "3 times of " << x << " is " << x << endl;
Or have times3() return double instead of passing by reference.
double times3(double x);

Unresolved External Symbol with a particular function in c++

I got the error message like the following:
Unresolved External Symbol error with calculatewinner Candidate
Here is my code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void headerforelection();
void getNamecalculatetotal(ifstream& in, string candidates[], double Votes[], double percentvotes[]);
void getNamecalculatetotal2(ifstream& in, string fname[], double Votes[], double percentvotes[]);
void allvotes(double []);
void Votesrecievedpercentage(ifstream& in, char Candidate[], double Votes[], string fname[], double percentvotes[]);
void Votesrecievedpercentage2(double Votes[], string fname[], double percentvotes[]);
void calculatewinner(string fname[], double Votes[]);
void headerforelection();
int main()
{
ifstream in = ifstream();
in.open("Votes.txt");
ofstream out = ofstream();
out.open("outputs.txt");
char winner();
char Candidate();
string fname[5];
double Votes[5];
double percentvotes[5];
double total = double();
headerforelection();
while (!in.eof())
{
getNamecalculatetotal(in, fname, Votes, percentvotes);
Votesrecievedpercentage2(Votes, fname, percentvotes);
}
allvotes(Votes);
calculatewinner(fname, Votes);
}
void getNamecalculatetotal(ifstream& in, string fname[], double Votes[], double percent[])
{
double total = double();
for (int i = 0; i < 5; i++)
{
in >> fname[i] >> Votes[i];
total = total + Votes[i];
}
for (int j = 0; j < 5; j++)
{
percent[j] = (Votes[j] / total) * 100;
}
}
void headerforelection()
{
std::cout << fixed << setfill(' ') << left << setw(10) << "Candidate"
<< right << setw(20) << setprecision(0) << "votes Recieved"
<< right << setw(20) << setprecision(2) << "% of Total Votes" << std::endl;
std::cout << endl;
}
void Votesrecievedpercentage2( double Votes[], string fname[], double percentvotes[])
{
for (int b = 0; b < 5; b++)
{
std::cout << fixed << setfill(' ') << left << setw(10) << fname[b]
<< right << setw(16) << setprecision(0) << Votes[b]
<< right << setw(16) << setprecision(2) << percentvotes << std::endl;
}
}
void allvotes(double Votes[])
{
double total = double();
for (int i = 0; i < 5; i++)
{
total = total + Votes[i];
}
std::cout << setfill(' ') << left << setw(22) << "Total" << setprecision(0) << total << std::endl;
}
void calculatewinner(string fname[], double Votes[])
{
{
int winner = 0;
for (int l = 0; l, 5; l++)
{
if (Votes[l] > Votes[winner])
{
winner = l;//3
}
}
}
char winner();
char Candidate();
std::cout << std::endl;
std::cout << Candidate << "Is The Winner" << fname << "." << std::endl;
}
First to answer your question, change these lines
char winner();
char Candidate();
everywhere in your code (in main as well as in calculatewinner) to the following:
char winner;
char Candidate;
By adding the paranthesis you are actually declaring a prototype to a function. Since you never define a function Candidate(void) the linker complains about the missing implementation. This would apply to char winner() also, but since you never use this "variable", the linker isn't concerned about this one.
Otherwise, your code is very broken. I am sure you are just learning C++, but your code is quite inconsistent in naming conventions as well as some other errors that should be addressed before doing something else.

Classes(Odometer)- Error Message

I was wondering if anyone could give me a clue as to what these error messages mean when I try to compile my code.
Here is the error I get:
in function 'int main()':
not match for 'operator<<'in 'std::operator<<[with_Traits = std::char_traits(((std::basic_ostr...
and it repeats for a while.
I want to post my full code just so you have a idea of what my assignment is, it not that long! =)
#include <iostream>
#include <cstdlib>
using namespace std;
class Odometer
{
public:
Odometer();
void reset();
void totalfuel();
void input_miles(int getmiles);
void Odometer::set_fuel_efficiency(double fuel_efficiency);
int gallonsUsed;
private:
int milesDriven;
double fuel_efficiency;
int getmiles;
};
Odometer::Odometer()
{
milesDriven = 0;
fuel_efficiency = 0;
}
void Odometer::reset()
{
milesDriven = 0;
}
void Odometer::totalfuel()
{
fuel_efficiency = (milesDriven/gallonsUsed);
}
void Odometer::input_miles(int miles_driven)
{
milesDriven = milesDriven + miles_driven;
}
void Odometer::set_fuel_efficiency(double Fuel_efficiency)
{
fuel_efficiency = Fuel_efficiency;
}
double Odometer::getgallons()
{
return milesDriven/fuel_efficiency;
}
// ======================
// main function
// ======================
int main()
{
// Two test trips
Odometer trip1, trip2;
trip1.reset();
trip1.set_fuel_efficiency(45);
trip1.input_miles(100);
cout << "For your fuel-efficient small car:" << endl;
cout << "After 100 miles, " << trip1.totalfuel() << " gallons used." << endl;
trip1.input_miles(50);
cout << "After another 50 miles, " << trip1.totalfuel() << " gallons used." << endl;
trip2.reset();
trip2.set_fuel_efficiency(13);
trip2.input_miles(100);
cout << "For your gas guzzler:" << endl;
cout << "After 100 miles, " << trip2.totalfuel() << " gallons used." << endl;
trip2.input_miles(50);
cout << "After another 50 miles, " << trip2.totalfuel() << " gallons used." << endl;
system("PAUSE");
return 0;
}
What would you expect cout << void to print?
totalfuel() returns void, and you're passing it as a parameter to cout::operator <<. Did you mean to return something from the method?
Perhaps:
double Odometer::totalfuel()
{
fuel_efficiency = (milesDriven/gallonsUsed);
return fuel_efficiency;
}
totalFuel() returns void. I think you meant to invoke the getgallons() method instead.

"overloaded member function not found" reference

I have a problem with my code. I tried to use reference to take my variables from method to main() function and i get this error : "overloaded member function not found". Please Help! :)
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
class Tworzenie_postaci {
public:
Tworzenie_postaci();
string Nazwa_Postaci();
int Wiek_Postaci();
int Staty_Postaci(int Final_Postac_Sila, int Final_Postac_Inteligencja);
int Staty_Postaci();
private:
};
Tworzenie_postaci::Tworzenie_postaci() {
}
string Tworzenie_postaci::Nazwa_Postaci()
{
string wpisz_nazwa;
cout << "Wybierz imie dla swojej postaci:\n";
cin >> wpisz_nazwa;
cout << "Nazwa twojej postaci to:\n" << wpisz_nazwa<< "\n";
return wpisz_nazwa;
}
int Tworzenie_postaci::Wiek_Postaci()
{
int wiek;
cout << "Ile twoja postac ma lat?\n";
cin >> wiek;
cout << "Wiec twoja postac ma " << wiek << " lat\n";
return wiek;
}
void Tworzenie_postaci::Staty_Postaci(int& _Postac_Sila, int& _Postac_Inteligencja) {
int Postac_Sila;
int Postac_Inteligencja;
cout << "Ile twoja postac ma sily? :\n";
cin >> Postac_Sila;
cout << "Twoja postac ma " << Postac_Sila << " sily \n";
cout << "Ile twoja postac ma inteligencji? : \n";
cin >> Postac_Inteligencja;
cout << "Twoja postac ma "<< Postac_Inteligencja << " inteligencji \n";
_Postac_Sila = Postac_Sila;
_Postac_Inteligencja = Postac_Inteligencja;
}
int main()
{
Tworzenie_postaci Postac;
string Final_Imie;
int Final_Wiek;
int Final_Postac_Sila;
int Final_Postac_Inteligencja;
Final_Imie = Postac.Nazwa_Postaci();
Final_Wiek = Postac.Wiek_Postaci();
Postac.Staty_Postaci(Final_Postac_Sila, Final_Postac_Inteligencja);
cout << "\n " << Final_Postac_Sila;
return 0;
}
Sorry for polish names of variables or functions but it is easier for me that way :P
The function is declared as:
int Staty_Postaci(int Final_Postac_Sila,
int Final_Postac_Inteligencja);
It is defined as:
void Tworzenie_postaci::Staty_Postaci(int& _Postac_Sila,
int& _Postac_Inteligencja)
{
...
}
As you can see, the types in the declaration and the definition don't match.
Change one of them to match the other.
Also, the return types must match.