The ERROR! starts from line 7 which is GradeBook::GradeBook(string name)(Type name is not Allowed)
I Wrote it the same as in the example cant understand why its not working and I am using visual studio 2015 to compile.
#include <iostream>
#include "Header.h"
using namespace std;
int main() {
GradeBook::GradeBook (string name) {
setCourseName(name);
}
void GradeBook::setCourseName(string name) {
coursename = name;
}
string GradeBook::getCourseName() {
return courseName;
}
void GradeBook::displayMessage() {
cout << "Welcome to the Grade Book\n" << getCourseName <<"!" << endl;
}
system("PAUSE");
return 0;
}
You need to move the definitions of the member functions of your class outside main.
#include <iostream>
#include "Header.h"
using namespace std;
GradeBook::GradeBook (string name) {
setCourseName(name);
}
void GradeBook::setCourseName(string name) {
coursename = name;
}
string GradeBook::getCourseName() {
return courseName;
}
void GradeBook::displayMessage() {
cout << "Welcome to the Grade Book\n" << getCourseName <<"!" << endl;
}
int main() {
system("PAUSE");
return 0;
}
We cannot define functions inside the another function in c++.
int main() {
GradeBook::GradeBook (string name) {
setCourseName(name);
}
void GradeBook::setCourseName(string name) {
coursename = name;
}
return 0;
}
but can declare function in another function.
and any other IDE also don't allow to define function in another function. You can define like this.
GradeBook::GradeBook (string name) {
setCourseName(name);
}
void GradeBook::setCourseName(string name) {
coursename = name;
}
int main(){
GradeBook b;
return 0;
}
Related
I am new to OOP and I have encountered a problem while writing my first code. I do not understand why I can not use one class as a part of another. And no I do not want that class to inherit another. One of the requirements is that I prevent object copying.
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Pilot
{
public:
Pilot(/*string x*/)
{
setName();
flight_hours = 0;
set_status(0);
}
void setName(/*string x*/)
{
cout<<"Unesi ime pilota: ";
getline(cin,name);
}
string getName()
{
return name;
}
void increase_flight_hours(int n)
{
flight_hours += n;
}
int get_flight_hours()
{
return flight_hours;
}
void set_status(bool b)
{
status;
}
bool get_status()
{
return status;
}
void display_pilot()
{
cout << name;
cout << "(", flight_hours, ")";
if (status)
cout << "-L" << endl;
else
cout << "-N" << endl;
}
Pilot (const Pilot&) = delete;
void operator=(const Pilot&) = delete;
private:
string name;
int flight_hours;
bool status;
};
#pragma once
#include"Pilot.h"
class Avion
{
public:
Avion ()
{
setName();
set_capacity();
}
void setName(/*string x*/)
{
cout << "Unesi ime aviona: ";
getline(cin, name);
}
string getName()
{
return name;
}
void set_capacity()
{
cout << "Unesite kapacitet aviona: ";
cin >> capacity;
}
int get_capacity()
{
return capacity;
}
Pilot get_captain()
{
return captain;
}
private:
string name;
Pilot captain;
Pilot copilot;
int capacity;
};
I get this error :
function "Pilot::Pilot(const Pilot &)" (declared at line 50 of "C:\Users\mjova\source\repos\Project1\Project1\Pilot.h") cannot be referenced -- it is a deleted function Project1 C:\Users\mjova\source\repos\Project1\Project1\Planes.h 36
One problem is here:
Pilot get_captain()
{
return captain;
}
which returns a copy of the captain, and you have expressly disallowed copying.
Return a const reference instead:
const Pilot& get_captain()
{
return captain;
}
(and don't try to copy what it returns).
There could also be some other copying-related code in "Planes.h"; it's not clear whether Avion is defined in that file or not.
Side note: since you can't copy Pilots, the captain and copilot members of Avion are problematic (you can't implement set_captain, for instance).
I suspect that you're going to want to change them to be pointers in the future, and let pilots exist without planes and vice versa.
I am new to C++ and am having issues when running this code, I have no idea where I have gone wrong.
I am just trying to make a simple Song class, then add and display an instance of the song object.
The error is saying that allot of my Methods are already defined in projectName.obj. I am also getting unresolved external symbol on IDSeed.
I am using visual studio 2017.
Main
#include "stdafx.h"
#include "Song.h"
#include "Song.cpp"
int main()
{
Song testSong("Evil Tram", "Catz N dogz");
testSong.setGenre("Tech House");
testSong.display();
return 0;
}
Song.cpp
#include "stdafx.h"
#include "Song.h"
Song::Song()
{
m_title = "";
m_album = "";//change to class
m_artist = "";//change to class
m_genre = "";//change to enum
m_ID = 0;
IDSeed = 0;
}
Song::Song(string title, string artist)
{
m_title = title;
m_album = "No Album";
m_genre = "No Genre";
m_artist = artist;
IDSeed++;
m_ID = IDSeed;
}
string Song::getTitle() const
{
return m_title;
}
string Song::getAlbum() const
{
return m_album;
}
string Song::getArtist() const
{
return m_artist;
}
string Song::getGenre() const
{
return m_genre;
}
int Song::getID() const
{
return m_ID;
}
void Song::setTitle(string title)
{
m_title = title;
}
void Song::setAlbum(string album)
{
m_album = album;
}
void Song::setArtist(string artist)
{
m_artist = artist;
}
void Song::setGenre(string genre)
{
m_genre = genre;
}
void Song::setID(int id)
{
m_ID = id;
}
void Song::display() const
{
cout << m_title << ", " << m_album << ", "
<< m_artist << ", " << m_genre << endl;
}
Song::~Song()
{
}
ostream & operator<<(ostream & out, Song & s)
{
out << s.m_title << ", " << s.m_album << ", "
<< s.m_artist << ", " << s.m_genre << endl;
return out;
}
istream & operator>>(istream & in, Song & s)
{
in >> s.m_title >> s.m_album >> s.m_artist >> s.m_genre;
return in;
}
Song.h
#pragma once
#include <iostream>
#include <string>
#include <ostream>
using std::string;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
class Song
{
private:
#pragma region Variables
string m_title;
string m_album;//change to class
string m_artist;//change to class
string m_genre;//change to enum
int m_ID;
static int IDSeed;
#pragma endregion
public:
Song();
Song(string title, string artist);
#pragma region Getters
string getTitle() const;
string getAlbum()const;
string getArtist()const;
string getGenre()const;
int getID()const;
#pragma endregion
#pragma region Setters
void setTitle(string title);
void setAlbum(string album);
void setArtist(string artist);
void setGenre(string genre);
void setID(int id);
#pragma endregion
#pragma region Methods
void display() const;
#pragma endregion
friend ostream& operator<<(ostream& out,
Song& s);
friend istream& operator>>(istream& in,
Song& s);
~Song();//destructer
};
Solved by removing the #include song.cpp and placing the static keyword in the cpp file instead of the .h
I've been doing c++ self study after taking two semesters of Java.
Below is a simple program which builds fine, but when I run it I get gradeBook2s displayInfo() method called twice. I'm sure its something basic I am missing, but any ideas why?
//GradeBook.h
#include<string>
#include<iostream>
using std::cout;
using std::endl;
using std::string;
string courseName;
int courseGrade;
class GradeBook {
public:
GradeBook(string name, int grade) {
setCourseName(name);
setCourseGrade(grade);
}
public:
void setCourseName(string name) {
courseName = name;
}
public:
string getCourseName() {
return courseName;
}
public:
void setCourseGrade(int score) {
courseGrade = score;
}
public:
int getCourseGrade() {
return courseGrade;
}
public:
void displayInfo(){
cout << "Course Name: " << getCourseName()
<< "Course grade: " << getCourseGrade()
<< endl;
}
};
//main.cpp
#include<iostream>
#include "GradeBook.h"
int main() {
GradeBook gradeBook1("Calculus 2", 90);
GradeBook gradeBook2("Chemistry", 80);
gradeBook1.displayInfo();
system("pause");
gradeBook2.displayInfo();
system("pause");
}
The problem that you have is that you are saving the variables courseName and courseGrade as global variables. Because of this, you are overwriting the previous class instance's data with the current, in this case, Chemistry. What you need to do is move the declaration of courseName and courseGrade inside the accessor private inside the class, like this:
class GradeBook {
private:
string courseName;
int courseGrade;
public:
//put your functions here
}
The locations where the code stores values is global, so all instances will appear the same.
Whenever I try to make an object and call a function on it, it doesn't seem to work.
I have no idea why, since I don't seem to have errors too.
I have searched around on here regarding constructors and the toString-method, but haven't find anything that worked.
I have tried to edit (distinct) the members in the constructor members,
Tried to rewrite the toString method.
Tried to make local object (with no pointer).
But it doesn't return me the things in the object that I created when calling the constructor.
Where does the problem situate in this problem?
Here is my code:
.h file:
#pragma once
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
class Store{
private:
int id;
string name;
string adress;
string telephone;
string btwNumber;
public:
int getId();
void setId(int);
string getName();
void setName(string);
string getAdress();
void setAdress(string);
string getTelephone();
void setTelephone(string);
string getBtwNumber();
void setBtwNumber(std::string);
string toString();
Store(int, string, string , string, string);
};
.cpp file:
// Store.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Store.h"
Store::Store(int idnum, string nameS, string adreS, string telephonE, string btwnummeR){
idnum = id;
nameS = name;
adreS = adress;
telephonE = telephone;
btwnummeR = btwNumber;
}
int Store::getId()
{
return id;
}
void Store::setId(int id){
this->id = id;
}
string Store::getName(){
return naam;
}
void Store::setName(string name){
this->naam = naam;
}
string Store::getTelephone(){
return telephone;
}
void Store:setTelephone(string telephone){
this->telephone = telephone;
}
string Store::getBtwNumber()
{
return btwNumber;
}
void Store::setBtwNumber(string btwNumber){
btwNumber = btwNumber;
}
string Store::getAdress(){
return adress;
}
void Store::setAdress(string adress){
this->adress = adress;
}
string Store::toString(){
stringstream s;
s << "Id: " << id << endl;
s << "Naam: " << name << endl;
s << "Adres: " << adress << endl;
s << "Telefoonnummer: " << telephone << endl;
s << "BTWnummer: " << btwNumber << endl;
return s.str();
}
int _tmain(int argc, _TCHAR* argv[])
{
Store *test = new Store (4, "Test", "test", "test", "test");
test->toString();
system("Pause");
return 0;
}
Your constructor is inversed: you are assigning member variables to constructor arguments and not vice versa.
nameS = name;
Should be
name = nameS;
And so on
The method toString does work, but it won't magically decide to output its return value to screen. You'll have to do it yourself:
std::cout << test->toString() << std::endl;
You'll need to add #include <iostream> on top of your cpp file.
#include <iostream>
#include <string>
using namespace std;
class BookData
{
string Title;
int Qty;
public:
void setTitle(string in_title) { Title = in_title;}
string setQty(int in_qty) { Qty = in_qty; }
string getTitle() { return Title; }
int getQty() { return Qty; }
};
int main()
{
BookData book;
book.setTitle("Starting Out with C++");
book.setQty(10);
cout << "Title is " << book.getTitle() << ".\n\n";
cout << "Quantity is " << book.getQty() << ".\n\n";
return 0;
}
When I compile all I get is an empty console. Any suggestions?
Change the return type of BookData::setQty() from string to void.
Without this change, it should still work fine. On my Linux machine, it crashes when setQty() is called with a return type of string and no string being returned.
Does the console close immediately? If so, put this before return 0;:
System("PAUSE");
Or:
std::cin.ignore();
Or (for MSVC++2010):
int temp;
std::cin >> temp;