i cannot understand what does theese error mean - c++

#include <iostream>
#include <sstream>
#include <cstring>
using namespace std;
class Student {
int age;
char first_name[30];
char second_name[30];
int standard;
public:
void set_age(int a) {
age=a;
}
void set_standard(int b){
standard=b;
}
void set_last_name(char temp[]) {
strcpy(second_name,temp);
}
void get_age() {
cout<<age;
}
void get_last_name(){
cout<<second_name;
}
void get_first_name() {
cout<<first_name;
}
void get_standard(){
cout<<standard;
}
};
void Student::set_first_name(char temp[]){
strcpy(first_name,temp);
}
int main() {
int age, standard;
string first_name, last_name;
cin >> age >> first_name >> last_name >> standard;
Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name);
st.set_last_name(last_name);
cout << st.get_age() << "\n";
cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
cout << st.get_standard() << "\n";
cout << "\n";
cout << st.to_string();
return 0;
}
errors
Solution.cpp:35:6: error: no declaration matches ‘void Student::set_first_name(char*)’
void Student::set_first_name(char temp[]){
^~~~~~~
Solution.cpp:35:6: note: no functions named ‘void Student::set_first_name(char*)’
Solution.cpp:6:7: note: ‘class Student’ defined here
class Student {
^~~~~~~
Solution.cpp: In function ‘int main()’:
Solution.cpp:48:8: error: ‘class Student’ has no member named ‘set_first_name’; did you mean ‘get_first_name’?
st.set_first_name(first_name);
^~~~~~~~~~~~~~
get_first_name
Solution.cpp:49:31: error: no matching function for call to ‘Student::set_last_name(std::__cxx11::string&)’
st.set_last_name(last_name);
^
Solution.cpp:18:10: note: candidate: ‘void Student::set_last_name(char*)’
void set_last_name(char temp[]) {

You have provided a definition of the method, but the method is not declared in the class.
Add this line inside the Student class to provide a declaration:
void set_first_name(char temp[]);

you don't declare your function in your class
add this line to class body and the error will disappear
void set_first_name(char temp[]);

You have several issues:
cout cannot print void.
you haven't declared a function but you try to define it.
#include <iostream>
#include <sstream>
#include <cstring>
using namespace std;
class Student {
int age;
char first_name[30];
char second_name[30];
int standard;
public:
void set_age(int a) {
age=a;
}
void set_standard(int b){
standard=b;
}
void set_last_name(const char* temp) {
strcpy(second_name,temp);
}
void get_age() {
cout<<age;
}
void get_last_name(){
cout<<second_name;
}
void get_first_name() {
cout<<first_name;
}
void get_standard(){
cout<<standard;
}
void set_first_name(const char* temp){
strcpy(first_name,temp);
}
};
int main() {
int age, standard;
string first_name, last_name;
cin >> age >> first_name >> last_name >> standard;
Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name.c_str());
st.set_last_name(last_name.c_str());
st.get_age();
st.get_last_name();
st.get_first_name();
st.get_standard();
cout << "\n";
//cout << st.to_string();
return 0;
}

You need to have something like void set_first_name(char temp[]) in the deceleration of your student class. Also, strings are not the same as char arrays and must be casted to that type. I recommend using strings in your class and not char arrays. This would be causing your last few errors.

I want to add to Oblivion's answer that you cannot cout first_name, second_name as well. First_name and second_name are pointers to the first element of the character array. Printing them would lead to just printing their addresses in the console.
You have to change these function declarations:
void get_last_name(){
cout<<second_name;
}
void get_first_name(){
cout<<first_name;
}
You need to add '\0' (end of string character) to the character arrays and then print it as a string. Check out how to do it by searching. You will learn a lot about working of character arrays and string.

Related

.Can there be a easier function defining - (to.string()) below

// The function which i was required to make was to.string() in the class,Which i had no idea how to make.This is an odd function(not comparing with the math one.)which returns value in two different types of data types i.e(string,integer).The only thing stuck me was assigning a variable after making (string to.string()) function//The return value of function is something like
[age,first_name,last_name,standard](without the square brackets with the commmasin the output)
p.s=need a simpler function without using vector header.
#include <iostream>
#include <sstream>
using namespace std;
class Student{
public :
void set_age(int no){
age_no=no;
}
void set_standard(int no){
std_no=no;
}
void set_first_name(string identity){
name_letter=identity;
}
void set_last_name(string identity2){
last_name_letter = identity2;
}
int get_age(){
return age_num;
}
int get_standard(){
return std_no;
}
string get_first_name(){
return name_letter;
}
string get_last_name(){
return last_name_letter;
}
private :
int age_no;
int std_no;
string name_letter;
string last_name_letter;
};
int main() {
int age, standard;
string first_name, last_name;
cin >> age >> first_name >> last_name >> standard;
Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name);
st.set_last_name(last_name);
cout << st.get_age() << "\n";
cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
cout << st.get_standard() << "\n";
cout << "\n";
cout << st.to_string();
return 0;
}
If you want to create a string with the format age, first_name, last_name, standard then you could do something like
class Student
{
public:
...
std::string to_string() const;
...
};
std::string Student::to_string() const
{
return std::to_string(get_age()) + ", " +
get_first_name() + ", "
get_last_name() + ", "
std::to_string(get_standard());
}
As an aside, I would suggest making all of your getter functions const for example
int get_age() const;
This denotes that the method will not mutate or modify any of the values of the class's member variables.

error in C++: out-of-line definition

CustomerInfo.cpp
#include "CustomerInfo.h" // <==== Funtion Definition
CustomerInfo::CustomerInfo() // <==== The Scope Resolution which is two colons :: gives me access to the class
{
newZipCode = 0;
}
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
{
newName = name;
newAddress = address;
newZipCode = zipCode;
}
CustomerInfo::~CustomerInfo()
{
}
string CustomerInfo::getName() const
{
return newName;
}
string CustomerInfo::getAddress() const
{
return newAddress;
}
int CustomerInfo::getZipCode() const
{
return newZipCode;
}
The main.cpp file
#include <iostream>
#include <string>
#include "CustomerInfo.h"
using namespace std;
int main()
{
string name;
string address;
int zipCode;
cout << "Enter your name: ";
getline (cin, name);
cout << "Enter your address" << endl;
getline (cin, address);
cout << "Enter your ZipCode: ";
cin >> zipCode;
CustomerInfo Real_1(name, address, zipCode);
cout << endl << " Name: " << Real_1.getName() << endl <<
"Address: " << Real_1.getAddress() << endl <<
"ZipCode: " << Real_1.getZipCode() << endl;
return 0;
}
The CustomerInfo.h file
#ifndef CUSTOMERINFO_H
#define CUSTOMERINFO_H
// Header ==> Function Declaration
#include <iostream>
#include <string>
using namespace std;
class CustomerInfo
{
public:
CustomerInfo(); // <==== Default Constructor
CustomerInfo(string, int); // <==== Overload Constructor
~CustomerInfo(); // <===== Destructor - Done using an object it will be destroyed out of memory'
string getName() const; // <==== Accessor Functions - Return member variables one value at a time. In addition, no void function will be used.
// getName - returns name of person
string getAddress() const;
// getAddress - returns address of person
int getZipCode() const;
// getZipCode - returns zipcode of person
private:
//Member Variables
string newName;
string newAddress;
int newZipCode;
}; // <=== Requires semicolon after brackets for classes
#endif // CUSTOMERINFO_H
The error I receive is out-of-line definition of 'CustomerInfo' does not match any declaration in 'CustomerInfo'
The following line is the error
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
Your problem seems to be that you define a constructor
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
which does not appear in the class definition (in CustomerInfo.h). The closest one is
CustomerInfo(string, int);
but it doesn't match the signature. Just replace it with
CustomerInfo(string name, string address, int zipCode);
to your class definition in the header file.

C++: error 'std::string' is private

So, for class, I have to write a program that simulates a horse race. It's our first major project involving classes. I'm stuck receiving the error mentioned in the subject. Now, I have struggled with pointers since they were introduced, so I have zero doubt this is where my overall problem lies. I've asked my professor for help, but he's not replying to my emails. I've also contacted friends, etc. and no one is getting back to me.
This is my header file (Horse.h):
#ifndef HORSE_H
#define HORSE_H
#include <string>
class Horse
{
private:
std::string name;
std::string jockey;
int maxSpeed;
int distanceTraveled;
int racesWon;
public:
Horse(std::string, std::string);
void runOneSecond(int);
void sendToGate();
void displayHorse (double);
};
#endif // HORSE_H
Here is my Horse.cpp:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Horse.h"
using namespace std;
Horse::Horse(string name, string jockey)
{
srand (time(NULL));
int maxSpeed = rand() % 30 + 1;
distanceTraveled = 0;
};
void Horse::runOneSecond(int maxSpeed)
{
srand (time(NULL));
distanceTraveled = rand() % maxSpeed;
};
void Horse::sendToGate()
{
distanceTraveled = 0;
};
void Horse::displayHorse(double raceDistance)
{
int percentage;
for (int i = 0; i < numberOfHorses; i++)
{
cout << "|";
}
};
And here is my main.cpp:
#include <iostream>
#include <string>
#include <cctype>
#include "Horse.h"
using namespace std;
int main()
{
double raceDistance = 0;
int numberOfHorses = 0;
char choice = 'Y';
string name;
string jockey;
cout << "Enter the number of horses for the race: ";
cin >> numberOfHorses;
Horse** horsePtr = new Horse* [numberOfHorses];
// Trouble section.
for (int i = 0; i < numberOfHorses; i++)
{
cout << "Fill in the name of the horse: ";
cin >> horsePtr[i]->name;
cout << "Fill in the name of the jockey: ";
cin >> horsePtr[i]->jockey;
}
cout << "How long should the race be (in meters): ";
cin >> raceDistance;
cout << endl;
cout << "Start!" << endl;
for (int i = 0; i < numberOfHorses; i++)
{
horsePtr[i]->sendToGate();
}
for (int i = 0; i < numberOfHorses; i++)
{
horsePtr[i]->displayHorse(raceDistance);
}
cout << "Show the next second of the race? ";
cin >> choice;
while(toupper(choice) == 'Y')
{
if (toupper(choice) == 'Y')
{
for (int i = 0; i < numberOfHorses; i++)
{
horsePtr[i]->runOneSecond(maxSpeed);
horsePtr[i]->displayHorse(raceDistance);
}
}
}
return 0;
}
Here is the error:
Horse.cpp: In member function ‘void Horse::displayHorse(double)’:
Horse.cpp:29:23: error: ‘numberOfHorses’ was not declared in this scope
for (int i = 0; i < numberOfHorses; i++)
^
In file included from main.cpp:4:0:
Horse.h: In function ‘int main()’:
Horse.h:8:17: error: ‘std::string Horse::name’ is private
std::string name;
^
main.cpp:25:25: error: within this context
cin >> horsePtr[i]->name;
^
In file included from main_dmj8t6.cpp:4:0:
Horse.h:9:17: error: ‘std::string Horse::jockey’ is private
std::string jockey;
^
main.cpp:27:25: error: within this context
cin >> horsePtr[i]->jockey;
^
main.cpp:55:35: error: ‘maxSpeed’ was not declared in this scope
horsePtr[i]->runOneSecond(maxSpeed);
^
You cannot access a private member of a class. In your example, your Horse class contains 5 private members:
class Horse
{
private:
std::string name;
std::string jockey;
int maxSpeed;
int distanceTraveled;
int racesWon;
public:
};
These private members can be accessed within the Horse class methods, and by any friend classes; however: nothing else can access them.
int main()
{
// Trouble section.
for (int i = 0; i < numberOfHorses; i++)
{
cout << "Fill in the name of the horse: ";
cin >> horsePtr[i]->name; <--
cout << "Fill in the name of the jockey: ";
cin >> horsePtr[i]->jockey; <--
}
}
On the two lines marked, you are attempting to access the private members of Horse and the compiler won't allow it.
Consider making those variables public to Horse, or providing setter functions for them:
class Horse
{
private:
std::string name;
std::string jockey;
int maxSpeed;
int distanceTraveled;
int racesWon;
public:
void SetName(std::string s) { name = s; }
void SetJockey(std::string s) { jockey = s; }
};
int main()
{
std::string jockeyName;
std::cout << "Enter a name for the jockey: ";
std::cin >> jockeyName;
Horse* h = new Horse;
h->SetJockey(jockeyName);
}
You are providing a public constructor for Horse that takes two std::string though (neither which you are using) so you could just pass the relevant information to the Horse:
Horse::Horse(std::string n, std::string j) : name(n), jockey(j)
{
// Other things...
}
int main()
{
Horse* h = new Horse("Phar Lap", "Jim Pike");
}
Note that my two examples are just matching your current code. Your arrays should be replaced by std::vector and pointers, if required, should be replaced by std::unique_ptr (or std::shared_ptr, whichever suits your needs).

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.