I am getting this error
error C2511: 'printlocation' : overloaded member function 'int (void)' not found in 'creature'
Here is the code:
Location.h
#ifndef location_h
#define location_h
class location
{
public:
setpoint(int,int,int);
getpoint(int,int,int);
private:
int x,y,z;
};
#endif
.cpp code below:
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include"location.h"
location::getpoint(int a,int b,int c)
{
x=a;
y=b;
z=c;
cout<<"give point x"<<x<<endl;
cout<<"give point y"<<y<<endl;
}
code for creature class :
#ifndef creature_h
#define creature_h
#include"location.h"
#include<string>
//using std::string;
using namespace std;
class creature
{
public:
creature();
moveto(location l);
getname(string n);
printlocation(string ,location );
private:
location lo;
string name;
};
#endif
code for creature.cpp in which there is an error:
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
using namespace std;
#include<string>
#include"creature.h"
#include"location.h"
creature::creature()
{
lo;
name;
}
creature::getname(string n)
{
name=n;
cout<<"enter name";
cin>>n;;
}
creature::printlocation()
:name(n),lo(l)
{
name=n;
lo=l;
cout<<lo.setpoint(int,int,int);
}
-----still error exists....---
#ifndef creature_h
#define creature_h
#include"location.h"
#include<string>
//using std::string;
using namespace std;
class creature
{
public:
creature();
void moveto(location l);
void getname(string );
void printlocation(string ,location );
private:
location lo;
string name;
};
#endif
.cpp
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
using namespace std;
#include<string>
#include"creature.h"
#include"location.h"
/*creature::creature()
{
lo;
name;
}*/
void creature::getname(string n)
{
name=n;
cout<<"enter name";
cin>>n;
}
void creature::printlocation()
:name(n),lo(l)
{
name=n;
lo=l;
cout<<l.getpoint(int,int,int);
}
main.cpp
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
#include"location.h"
#include"creature.h"
#include<string>
main()
{
string n;
int x,y,z;
location l;
l.getpoint(x,y,z);
creature c;
c.getname(n);
c.printlocation(n,l);
return 0;
}
Declaration of function has to specify it's return type (and correct namespace for arguments, since I don't see using namespace std):
void printlocation( std::string ,location );
Add the same for other declarations:
void creature();
void moveto(location l);
void getname(string n);
Then call printlocation this way:
std::string s;
location l;
//... give some value to s and l ...
printlocation( s, l);
Also change its name to conform to cammel case: printLocation, is better.
All your member function declarations have no return type. For example
class location
{
public:
setpoint(int,int,int);
getpoint(int,int,int);
//...
Ot
class creature
{
public:
//...
moveto(location l);
getname(string n);
printlocation(string ,location );
//...
You have to specify return types of the functions.
For example
void setpoint(int,int,int);
Also function getpoint logically more corresponds to function setpoint because it sets values of data members of an object
location::getpoint(int a,int b,int c)
{
x=a;
y=b;
z=c;
cout<<"give point x"<<x<<endl;
cout<<"give point y"<<y<<endl;
}
So I would rename the function as setpoint. For example
void location::setpoint(int a,int b,int c)
{
x=a;
y=b;
z=c;
cout<<"give point x"<<x<<endl;
cout<<"give point y"<<y<<endl;
}
After you have fixed the declaration errors mentioned in the already existing answers, give all of your definitions in the .cpp files the same signature as used in the declaration:
// NOTE the parameter defintions!!
void creature::printlocation(string n,location l) {
name=n;
lo=l;
cout<<lo.setpoint(1,2,3);
}
Please also note that the member initializer syntax is only valid for usage in constructor functions:
void creature::printlocation() : name(n),lo(l)
// ^^^^^^^^^^^^^^^ This is wrong!!!
UPDATE:
You probably meant to provide a parametrized constructor function for creature
class creature {
public:
creature() (string n,location l) : name(n),lo(l) {}
// ...
};
Related
So I am using Visual Studio to make a simple program for intro to Object Oriented Programming. I am using C++ language to do this, in Netbeans with JAVA OPP isn't so complicated but I am having trouble here. I have to make a simple Object, I chose to make my object called Movie. I made a Movie.h and Movie.cpp file. I included .h's extensions to my Movie.cpp and main.cpp but when I create the object in my main or try to i keep getting errors and underlines because my .cpp file is not recognizing my variables declared in .h, it keeps saying the variable is undefined.
So visual studio's wants to help me out and I followed their method so in my .h files they said they will define my methods for me, meaning they will set it up for me and when I clicked it i get this format
string Movie::getName()
{
return string();
}
While I am using this format for this function
string getName(){
return name;
}
My variables keeps getting red underlined saying they are undefined.
My Movie.h file
#pragma once
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z );
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
My Movie.cpp File
#include "Movie.h"
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void setLength(int x) {
length = x;
}
void setRating(double y) {
rating = y;
}
void setName(string z) {
name = z;
}
//Get functions
int getLength() {
return length;
}
double getRating() {
return rating;
}
string getName() {
return name;
}
My .main
#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1();
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Now I havent worked on my toString method yet but I can't because I cant figure out what the real problem is, why is my .cpp not recognizing my variables? Is my format wrong? Is the visual studio's format right because I ran with their format and got bunch of errors as well or am I declaring them wrong or something? Thank you!
Main.cpp
//#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1;
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Movie.h
#pragma once
#ifndef Movie_H
#define Movie_H
#include <string>
#include <iostream>
using namespace std;
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z);
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
#endif
Movie.cpp
#include "Movie.h"
//#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void Movie::setLength(int x) {
length = x;
}
void Movie::setRating(double y) {
rating = y;
}
void Movie::setName(string z) {
name = z;
}
//Get functions
int Movie::getLength() {
return length;
}
double Movie::getRating() {
return rating;
}
string Movie::getName() {
return name;
}
I'm still a noobie in c++ so I am not to skilled in debugging yet. Just trying to figure out how to fix this compilation error.
CruiseShip.cpp:11: error: expected ā)ā before ānā
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
int passengers;
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include "Ship.h"
#include <string>
using namespace std;
//class Ship;
class CruiseShip:public Ship{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif
You have 3 errors:
1 and 2. You don't declare print and CruiseShip (The constructor) as part of the class CruiseShip when you define them. You need to:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y) {
virtual void CruiseShip::print() {
3, you dont have a namespace Ship so this is unnecessary:
Ship::Ship s; // This only needs to be Ship s <- NameSpace::ObjectType nameOfObject;
After this it will compile http://ideone.com/wJ6mPO. It will not link however, because you have undefined references to all of the functions you have yet to define.
can sombody explain to me why my code will not work, and how to fix it thanks :)
I keep recieving this error :
no 'int burrito::setName()' member function declared in class 'burrito'
My goal is to call a function from a different class file
My main.cpp :
#include <iostream>
#include "burrito.h"
using namespace std;
int main()
{
burrito a;
a.setName("Ammar T.");
return 0;
}
My class header (burrito.h)
#ifndef BURRITO_H
#define BURRITO_H
class burrito
{
public:
burrito();
};
#endif // BURRITO_H
My class file (burrito.cpp):
#include "burrito.h"
#include <iostream>
using namespace std;
burrito::setName()
{
public:
void setName(string x){
name = x;
};
burrito::getName(){
string getName(){
return name;
};
}
burrito::variables(string name){
string name;
};
private:
string name;
};
Your code is a mess. You need to write function prototypes in the header file and function definitions in the cpp file. You are missing some basic coding structures. See below and learn this pattern of coding:
This code should work and enjoy burritos !
main():
#include <iostream>
#include "Header.h"
int main()
{
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
getchar();
return 0;
}
CPP file:
#include "Header.h"
#include <string>
void burrito::setName(std::string x) { this->name = x; }
std::string burrito::getName() { return this->name; }
Header file:
#include <string>
class burrito
{
private:
std::string name;
public:
void setName(std::string);
std::string getName();
//variables(string name) {string name;} // What do you mean by this??
};
Your poor little burrito is confused. Confused burritos can't help much.
You may want your burrito declaration as:
class Burrito
{
public:
Burrito();
void set_name(const std::string& new_name);
std::string get_name() const;
private:
std::string name;
};
The methods could be defined in the source file as:
void
Burrito::set_name(const std::string& new_name)
{
name = new_name;
}
std::string
Burrito::get_name() const
{
return name;
}
The header file only has a constructor for the class. The member functions
setName(string) and getName()
are not declared in the header file and that is why you get the error.
Also, you need to specify the return type for functions.
One way to do this would be
//Header
//burrito.h
class burrito{
private:
string burrito_name;
public:
burrito();
string getName();
void setName(string);
}
//burrito.cpp
#include "burrito.h"
#include <iostream>
using namespace std;
string burrito::getName()
{
return burrito_name;
}
void burrito::setName(string bname)
{
bname =burrito_name;
}
This is a simple example for class in C++,
Save this in burrito.cpp file then compile and run it:
#include <iostream>
#include <string>
using namespace std;
class burrito {
public:
void setName(string s);
string getName();
private:
string name;
};
void burrito::setName(string s) {
name = s;
}
string burrito::getName() {
return name;
}
int main() {
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
return 0;
}
So, I tried to make this code :
#include <iostream>
using namespace std;
class BuckysClass{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main()
{
BuckysClass bo;
bo.setName("Buckingham Palace");
cout << bo.getName();
return 0;
}
BECOMING TO A SEPARATED CLASS like this :
#include "BuckysClass.h"
#include <iostream>
#include <string>
using namespace std;
int main (){
BuckysClass bo;
bo.setName("Buckingham Palace");
cout << bo.getName();
return 0;
}
==============
#ifndef BUCKYSCLASS_H
#define BUCKYSCLASS_H
class BuckysClass
{
public:
void setName(string x);
string getName();
private:
string name;
};
#endif // BUCKYSCLASS_H
=============
#include "BuckysClass.h"
#include <iostream>
#include <string>
using namespace std;
BuckysClass::BuckysClass()
{
}
void setName(string x){
name = x;
}
string getName(){
return name;
}
When I run the first code, I succeed,
but I got error when running the separated class code,
help me find out what's wrong ???
I tried to use different code,
but it seems I can't find the reason,
the closest reason I believe, is the main doesnt call the function on the separated class properly.
If you write this code:
void setName(string x){
name = x;
}
string getName(){
return name;
}
The compiler interprete it as two functions called setName and getName, it has no idea they are member functions of your BuckysClass class.
You have to precise it with the following syntax:
void BuckysClass::setName(string x){
name = x;
}
string BuckysClass::getName(){
return name;
}
Additionally, here you are defining a default constructor:
BuckysClass::BuckysClass()
{
}
But you didn't put it in the class prototype. You have to add it somewhere in the class prototype definition in your .h file, or your compiler won't recognize it:
class BuckysClass
{
public:
BuckysClass(); // Default constructor.
void setName(string x);
string getName();
private:
string name;
};
I'm trying to make a "grade book".
I have a header file (GradeBook.h), and a main.cpp file.
I'm having trouble getting the value of a "midtermExamGrade" value to pass given the follow parameters:
GradeBook.h file:
#include <string>
#include <iostream>
using namespace std;
class GradeBook
{
public:
void setMidtermExamGrade(double grade)
{
double midterm_exam_grade = grade;
}
double getMidtermExamGrade()
{
return midterm_exam_grade;
}
private:
double grade;
} //end class GradeBook
Your private member is not being initialized correctly because your setter and getter are wrong. Try this:
#include <string>
#include <iostream>
using namespace std;
class GradeBook
{
public:
void setMidtermExamGrade(double grade)
{
midterm_exam_grade = grade; // use the private member
}
double getMidtermExamGrade()
{
return midterm_exam_grade;
}
private:
double midterm_exam_grade; // change here to match tha names in setter and getter
} //end class GradeBook