Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've seen this all over the site and still nothing has been able to fix my problem. this is the only error I have and I have no idea how to fix this. I've tried commenting out different sections of the code just to see what happens and still nothing but errors. What is going on?
Her is the build error as well
error C2084: function 'HeroProfile::HeroProfile(void)' already has a body
Main.cpp
#include <iostream>
#include <string>
#include "HeroProfile.h"
using namespace std;
int main()
{
string name;
int race;
double weight;
cout << "Enter your Hero's name:";
cin>> name;
cout << "Enter your Hero's Race (1:Elf 2:Human 3 Dwarf)";
cin >> race;
cout << "Enters your Hero's weight ( in pounds):";
cin >> weight;
HeroProfile Hero_1(name, race, weight);
cout << endl << "hero's Name: " << Hero_1.getName() << endl <<
"Race: " << Hero_1.getRace() << endl <<
"Weight:" << Hero_1.getWeight() << endl;
cout << endl;
cout << "Enter your Hero's Name:";
cin >> name;
cout << "Enter your Race (1:Elf 2:Human 3 Dwarf)";
cin >> race;
cout <<"Enter your Hero's weight (in pounds)";
cin >> weight;
HeroProfile Hero_2(name, race, weight);
Hero_2.setName(name);
Hero_2.setRace(race);
Hero_2.setWeight(weight);
cout << endl << "Hero's Name: " << Hero_2.getName() << endl <<
"Race: " << Hero_2.getRace() << endl << "Weight: " << Hero_2.getWeight() << endl;
return 0;
}
The header File
#include <iostream>
#include <string>
using namespace std;
#ifndef HeroProfiel_H
#define HeroProfile_H
class HeroProfile
{
public:
//default constructor
HeroProfile();
//overlaod constructor
HeroProfile(string, int, double);
//destructor
~HeroProfile();
//Accesor functions
string getName() const;
// get name - returns name of patient
int getRace() const;
// getHeight-returns height of patient
double getWeight() const;
// getWeight- returns weight of patient
//Mutator Functions
void setName(string);
//set name of patient
//#param string - name of patient
void setRace(int);
//setHeight-sets heigh tof patient
//#param int- height iof patient
void setWeight(double);
//setWeight-sets weight of patiend
//#param double-weight of patient
private:
//Memeber variables
string newName;
int newRace;
double newWeight;
};
#endif
and the second .cpp file
#include "HeroProfile.h"
eroProfile::HeroProfile() //Default constructor
{
newRace = 0;
newWeight = 0.0;
}
HeroProfile::HeroProfile(string name, int race, double weight) //Overloaded Constructor
{
newName = name;
newRace = race;
newWeight = weight;
}
HeroProfile::HeroProfile()
}
}
//Accessors
string HeroProfile::getName() const
{
return newName;
}
int HeroProfile::getRace() const
{
return newRace;
}
double HeroProfile::getWeight() const
{
return newWeight;
}
//Mutators
void HeroProfile::setName(string name)
{
newName = name;
}
void HeroProfile::setRace(int race)
{
newRace = race;
}
void HeroProfile::setWeight(double weight)
{
newWeight = weight;
}
As others have noted, you have 2 definitions in your second cpp file for the HeroProfile default constructor HeroProfile::HeroProfile():
The first is
HeroProfile::HeroProfile() //Default constructor
{
newRace = 0;
newWeight = 0.0;
}
and the second is
HeroProfile::HeroProfile()
}
}
Based on the fact I don't see one, you probably intended for the second one to be your class destructor (as declared in your header file but not defined in your cpp file), in which case you should replace it with this:
HeroProfile::~HeroProfile()
}
}
I hope you didn't get confused by the fact that HeroProfile::HeroProfile(void) and HeroProfile::HeroProfile() are the same thing, so I thought I should point it out.
The message means exactly what it says. Ignoring typos, the first and third definitions in your second .cpp are for the same function.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 22 days ago.
Improve this question
I'm currently learning how to code object oriented and I've created 2 classes (Applicant and OfferLetter) and trying to inherit those 2 classes into a third class (Software Engineer). I've gotten it structured for the most part however im getting an error code on my compiler that says "No default constructor exists for the class "OfferLetter". What can i do to fix this error?
#include <iostream>
using namespace std;
class Applicant{
private:
string Name;
string JobTitle;
string Degree;
int YearsOfExpiremce;
int Age;
public:
//Name
void setName(string name){
Name = name;
}
string getName(){
return Name;
}
//JobTitle
void setJobTitle(string jobtitle){
JobTitle = jobtitle;
}
string getJobTitle(){
return JobTitle;
}
//Degree
void setDegree(string degree){
Degree = degree;
}
string getDegree(){
return Degree;
}
//Expirence
void setExpirence(int expirence){
YearsOfExpiremce = expirence;
}
int getExpirence(){
return YearsOfExpiremce;
}
//Age
void setAge(int age){
Age = age;
}
int getAge(){
return Age;
}
//Constructor
Applicant(string name, string jobtitle, string degree, int expirence, int age){
Name = name;
JobTitle = jobtitle;
Degree = degree;
YearsOfExpiremce = expirence;
Age = age;
}
};
class OfferLetter{
private:
int BaseSalary;
int SignOnBonus;
int Stocks;
int Incentives;
public:
//Base Salary
void setSalary(int salary){
BaseSalary = salary;
}
int getSalary(){
return BaseSalary;
}
//Sign-On Bonus
void setBonus(int bonus){
SignOnBonus = bonus;
}
int getBonus(){
return SignOnBonus;
}
//Stocks
void setStocks(int stocks){
Stocks = stocks;
}
int getStocks(){
return Stocks;
}
//Incentives
void setIncentives(int incentives){
Incentives = incentives;
}
int getIncentives(){
return Incentives;
}
//Constructor
OfferLetter(int salary, int bonus, int stocks, int incentives){
BaseSalary = salary;
SignOnBonus = bonus;
Stocks = stocks;
Incentives = incentives;
}
};
class SoftwareEngineer: public Applicant, public OfferLetter{
public:
string ProgrammingLanguage;
string JobDescription;
SoftwareEngineer(string name, string jobtitle, string degree, int expirence, int age, int salary, int bonus,
int stocks, int incentives, string language, string jd)
:Applicant(name, jobtitle, degree, expirence, age)
:OfferLetter(salary, bonus, stocks, incentives)
{
ProgrammingLanguage = language;
JobDescription = jd;
}
void DisplaySEOfferLetter(){
cout << "Congratulations on your job offer, Here is your total compensation. " << endl;
cout << "Please respond to us in the next 5 days to confirm your Job Offer." << endl;
cout << endl;
cout << "Total Compensation " << endl;
cout << "-------------------" << endl;
cout << "Role: Software Engineer " << endl;
cout << "Job Description: $" << endl;
cout << "Base Salary : $" << endl;
cout << "Sign-On Bonus: $" << endl;
cout << "Stocks : $" << endl;
cout << "Incentives : $" << endl;
cout << endl;
}
};
I think the problem is not the default constructor missing, but that you have a syntax error in your code:
SoftwareEngineer(string name, string jobtitle, string degree,
int expirence, int age, int salary, int bonus,
int stocks, int incentives, string language, string jd)
:Applicant(name, jobtitle, degree, expirence, age)
:OfferLetter(salary, bonus, stocks, incentives)
{
}
You attempt to create two constructor initializer lists, which is wrong. A constructor have a single comma-separated list:
SoftwareEngineer(string name, string jobtitle, string degree,
int expirence, int age, int salary, int bonus,
int stocks, int incentives, string language, string jd)
: Applicant(name, jobtitle, degree, expirence, age), // Note comma here
OfferLetter(salary, bonus, stocks, incentives) // No colon on this line
{
}
Note that you can initialize member variables using the initializer list as well, and it's recommended to use that instead of assignment inside the constructor body:
SoftwareEngineer(string name, string jobtitle, string degree,
int expirence, int age, int salary, int bonus,
int stocks, int incentives, string language, string jd)
: Applicant(name, jobtitle, degree, expirence, age), // Note comma here
OfferLetter(salary, bonus, stocks, incentives), // No colon on this line
ProgrammingLanguage(language), // Added initialization
JobDescription(jd) // of member variables
{
// Now this can be totally empty
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I need to calculate the average sales for each salesperson from a text file. My code will produce the correct output as the sample, but will not work if I add more information to the file without manually modifying the code.
I'm completely stuck on this, tried many different methods that I know but they doesn't completely satisfy the question. This is what I have so far.
Edit:
Is there a way to read the file, then add together all the amount that have the same name?
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class Sales
{
public:
int id;
string name;
int salesCount = 1;
double salesAmount;
double avgSalesAmount;
};
void read()
{
int i = 0;
Sales sale[4];
fstream read;
read.open("Sales.txt");
while(!read.eof())
{
read.ignore();
getline(read, sale[i].name);
read >> sale[i].salesAmount;
if (sale[i].name == sale[i-1].name)
{
sale[i-1].salesAmount = sale[i-1].salesAmount + sale[i].salesAmount;
sale[i-1].salesCount++;
sale[i].salesCount = 0;
}
i++;
}
read.close();
for (int x = 0; x < 4; x++)
{
if (sale[x].salesCount != 0)
{
sale[x].avgSalesAmount = sale[x].salesAmount / sale[x].salesCount;
cout << sale[x].name << endl;
cout << sale[x].salesCount << endl;
cout << sale[x].avgSalesAmount << endl;
}
}
}
int main()
{
read();
return 0;
}
Sample file "Sales.txt":
Matthew
123.45
Matthew
432.15
Brown
89.95
Cook
500.00
Sample Output: https://i.stack.imgur.com/QS6jE.png
Current Output (No Formatting): https://i.stack.imgur.com/QTA7K.png
This is caused by the static person count in your code, please try to replace the 4 with a larger constant.
const int PERSON_COUNT = 100;
void read()
{
int i = 0;
Sales sale[PERSON_COUNT];
fstream read, read2;
read.open("Sales.txt");
while(!read.eof())
{
read.ignore();
getline(read, sale[i].name);
read >> sale[i].salesAmount;
if (sale[i].name == sale[i-1].name)
{
sale[i-1].salesAmount = sale[i-1].salesAmount + sale[i].salesAmount;
sale[i-1].salesCount++;
sale[i].salesCount = 0;
}
i++;
}
read.close();
for (int x = 0; x < PERSON_COUNT; x++)
{
if (sale[x].salesCount != 0)
{
sale[x].avgSalesAmount = sale[x].salesAmount / sale[x].salesCount;
cout << sale[x].name << endl;
cout << sale[x].salesCount << endl;
cout << sale[x].avgSalesAmount << endl;
}
}
}
Note that using the variable-length array (VLA) in your functions is not recommended practice in most cases since it's a non-standard extension that doesn't work in all implementations. Please consider using std::vector<int> or other dynamic array solutions.
EDIT:
As Ted Lyngmo suggests, the code above is almost never the correct solution if the input amount is unknown during your coding period. But I'd keep that because it will need little effort for you to fix it if your scenario is simple enough.
Below is my humble refactor for you as a clearer solution. Hope this can enlighten you to build a better program on it.
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
#include <vector>
using namespace std;
class Sales {
public:
int id;
string name;
int salesCount = 1;
double salesAmount;
double avgSalesAmount;
};
void read() {
vector<Sales> sale;
ifstream fin("Sales.txt");
int i = 0;
string name;
double salesAmount;
while (fin >> name) {
fin >> salesAmount;
if (sale.empty() || sale.back().name != name) {
sale.push_back(Sales{i++, name, 1, salesAmount, 0});
} else {
sale.back().salesCount++;
sale.back().salesAmount += salesAmount;
}
}
fin.close();
for (auto &s : sale) {
s.avgSalesAmount = s.salesAmount / s.salesCount;
cout << s.name << endl;
cout << s.salesCount << endl;
cout << fixed << setprecision(2) << s.avgSalesAmount << endl;
cout << "---" << endl;
}
}
int main() {
read();
return 0;
}
test case:
A
1.00
A
2.00
A
3.00
B
2.00
B
3.00
C
4.00
A
3
2.00
---
B
2
2.50
---
C
1
4.00
---
EDIT 2:
If your data doesn't guarantee that records of the same person will come up in a row, in the simplest solution, please refer to std::map and use std::map<std::string, Sale> records to retrieve the person's record.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
Hi this is my first subject (question) at stack overflow i've tried an project about c++ classes at Code::Blocks but something went wrong
#include <iostream>
using namespace std;
class char1
{
public:
string charName;
float charLength;
void printName()
{
cout<<"char name is"<<charName;
}
};
int main()
{
int charNAME;
float charLENGTH;
cout<<"write your char's name"<<endl;
cin>>charNAME;
cout<<"write your char's length"<<endl;
cin>>charLENGTH;
char1 name;
char1 length;
name.charName=charNAME;
length.charLength=charLENGTH;
return 0;
}
when i run program it asks me char's name i write something, after it asks char's length
but program end there i cant do anything
here is picture for help
Some fixes for your code to show you how to use your class in practice (with some
c++ coding tips).
#include <string>
#include <iostream>
// using namespace std; <== don't do this
// https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
//class char1
class Character // <== give classes clear names, makes code more readable
{
public:
std::string name;
float length;
// I understand you want to do this to test stuff.
// but from a design point of view printing is not something
// a character can do to himself
/*
void printName()
{
cout << "char name is" << charName;
}
*/
};
// bit offtopic but :
// if you want to add support for printing your character
// its more common to overload operator<< for streams like this
std::ostream& operator<<(std::ostream& os, const Character& character)
{
os << "Your character's name = " << character.name << ", and his/her length = " << character.length << "\n";
return os;
}
int main()
{
// int charNAME; in your class this is a string, be consistent.
// float charLENGTH;
// make an instance of the Character class
Character character;
std::cout << "Enter your char's name : "; // << endl; avoid using endl use "\n" if you want a newline
std::cin >> character.name;
std::cout << "Enter your char's length : "; // << endl;
std::cin >> character.length;
std::cout << "\n";
std::cout << character; // this will now call your overloaded << operator
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This is an assignment i am trying to figure out:
Create a new project named Project3 and create a class named Rover
Within the Rover class, specify the following member instance variables:
name(string)
x position on a grid (integer)
y position on a grid (integer)
direction by compass – N, S, E, or W (String)
speed (0 – 5 meters per second, integer)
Within the Rover class, specify the following methods:
Default constructor – set the rover’s position to (0,0), its speed to 0, its direction to
North.
Constructor that receives parameters to initialize all five instance variables described above
Setter methods for each instance variable
Getter methods for each instance variable
getRoverData – returns a string that displays values for each instance variable of the
current rover object, placing each value on a separate line, as follows:
Rover name: A
X-position: 0
Y-position: 0
Direction: E
Speed: 1
Create a class client (main) that creates an array of the a maximum of five rovers and gets the initial
values for all rovers from the user. After the user specifies values for each rover, display a summary
of the rover’s values as shown above.
I have about a billion errors and i dont know why.
using namespace std;
class Rover {
private:
string name;
int x;
int y;
string direction;
int speed;
int position[10][10];
public:
void Rover();
void constructor(string name, int x, int y, string direction, int speed);
void setName(string name);
void setX(int x);
void setY(int y);
void setDirection(string direction);
void setSpeed();
string getName();
int getX();
int getY();
string getDirection();
int getSpeed();
string getRoverData();
};
void Rover::Rover() {
r1.position[0][0];
r1.speed = 0;
r1.direction = "N";
}
string Rover::getRoverData() {
cout << "Rover name: " << r1.getName() << endl;
cout << "X-position: " << r1.getX() << endl;
cout << "Y-position: " << r1.getY() << endl;
cout << "Direction: " << r1.getDirection() << endl;
cout << "Speed: " << r1.getSpeed() << endl;
}
void Rover::constructor(string name1, int x1, int y1, string direction1, int speed1) {
r1.name = name1;
r1.x = x1;
r1.y = y1;
r1.direction = direction1;
r1.speed = speed1;
}
void Rover::setName(string name) {
r1.name = name;
}
void Rover::setX(int x) {
r1.x = x;
}
void Rover::setY(int y) {
r1.y = y;
}
void Rover::setDirection(string direction) {
r1.direction = direction;
}
void Rover::setSpeed(int speed) {
r1.speed = speed;
}
string Rover::getName() {
return name;
}
int Rover::getX() {
return x;
}
int Rover::getY() {
return y;
}
string Rover::getDirection() {
return direction;
}
int Rover::getSpeed() {
return speed;
}
int main(int argc, char** argv) {
string name;
int x;
int y;
string direction;
int speed;
Rover r1;
r1.constructor("Yoda", 3, 3, "N", 3);
cout << "Enter name for Rover: ";
cin >> name;
r1.setName(name);
cout << "Enter its x position: ";
cin >> x;
r1.setX(x);
cout << "Enter its y position: ";
cin >> y;
r1.setY(y);
cout << "Enter direction N,E,S,W: ";
cin >> direction;
r1.setDirection(direction);
cout << "Enter its speed: ";
cin >> speed;
r1.setSpeed(speed);
r1.getRoverData();
return 0;
}
Your example appears incomplete. I'm guessing you just missed including the following lines in your post
#include <string>
#include <iostream>
using namespace std;
First, constructors do not have a return type so void Rover(); makes no sense. Remove void and you're golden there.
Second, what exactly do you think r1 is supposed to be? The compiler should tell you the identifier is undefined because it isn't. remove r1. from your member functions (i.e. anything function starting with Rover::. and you're golden there.
Third, what do you think r1.position[0][0] is going to do? It's just an expression that does nothing. Even position[0][0] is not going to do anything. Perhaps you want to initialize the array somehow but you haven't provided enough information to determine what you're trying to accomplish with it.
Fourth, the member function void Rover::setSpeed(int) has not been declared within the Rover class. Did you forget something? Based on your code it should be
int Rover::getSpeed()
{
return speed;
}
Fifth, void Rover::setSpeed(); doesn't make much sense unless it actually accepts an argument.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to link my student_info structure with my read_name function but I am having issues getting it to work properly and it won't compile. The errors I am getting now are error: ‘first_name’ was not declared in this scope and error: ‘last_name’ was not declared in this scope. I declared them in the structure however.
Here's my code:
#include <iostream>
using namespace std;
//Place your structure here for Step #1:
struct student_info
{
char first_name[15];
char last_name[15];
char crn[15];
char course_designator[15];
int section;
};
//Place any prototypes that use the structure here:
void read_name(student_info & first_name[], student_info & last_name[])
{
cout << "enter first name" << endl;
cin.getline(first_name, 15, '\n');
cout << "enter last name" << endl;
cin.getline(last_name, 15, '\n');
first[0] = toupper(first_name[0]);
last[0] = toupper(last_name[0]);
cout << "your name is " << first_name << " " << last_name << endl;
}
int main()
{
//For Step #2, create a variable of the struct here:
student_info student;
read_name(first_name, last_name);
return 0;
}
Things you can do to fix your problem.
Change read_name to take a reference to a student_info.
void read_name(student_info & student)
Change the implementation of read_name to read the data into the first_name and last_name members of info.
void read_name(student_info & student)
{
cout << "enter first name" << endl;
cin.getline(student.first_name, 15, '\n');
cout << "enter last name" << endl;
cin.getline(student.last_name, 15, '\n');
first[0] = toupper(student.first_name[0]);
last[0] = toupper(student.last_name[0]);
cout << "your name is " << student.first_name << " "
<< student.last_name << endl;
}
From main, call read_name using student as argument.
int main()
{
//For Step #2, create a variable of the struct here:
student_info student;
read_name(student);
return 0;
}