I am trying to call a function inside a class, when I try I get the error "no operator << matches these operands" right before instructor.displayMessage(). Also, am I calling instructor.displayMessage() correctly? I am new to c++
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
GradeBook gradeBook1("CS101 Introduction to C++ Programming");
GradeBook gradeBook2("CS102 Data Structures in C++");
GradeBook instructor("");
instructor.setInstructorName();
cout << "gradeBook1 created for course: \n" << gradeBook1.getCourseName() << instructor.displayMessage()
<< "\ngradeBook2 created for course: \n" << gradeBook2.getCourseName()
<< endl;
cout << "\nPress any key to exit" << endl;
getchar();
}
Header:
#include <string>
using namespace std;
class GradeBook{
public:
GradeBook(string);
void setCourseName(string);
string getCourseName();
void displayMessage();
void setInstructorName();
string getInstructorName();
private:
string courseName;
string instructorName;
};
I didnt include the functions because I dont think they are part of the problem.
void displayMessage();
This function does not return anything, yet you try to print its return value here:
cout << "gradeBook1 created for course: \n" << gradeBook1.getCourseName() << instructor.displayMessage()
If it actually should return something, then you have to declare it with the correct return type, for example
string displayMessage();
However the name suggests that the function itself prints the output already. So maybe you simply want to call it, like this:
instructor.displayMessage();
in a single line.
If you provide the implementation of displayMessage() I might give a more precise answer.
Related
New to C++
My understanding is endl will add a new line. So with the following piece of code:
#include <iostream>
using namespace std;
void printf(string message);
int main()
{
cout << "Hello" << endl;
cout << "World" << endl;
printf("Hello");
printf("World");
return 0;
}
void printf(string message) {
cout << message << endl;
}
I expect the output to be:
Hello
World
Hello
World
But, strangely, the output is:
Hello
World
HelloWorld
Looks like, when called from the user-defined method, endl is not adding new line..??
What is wrong with my understanding here. Please advise.
The problem is that due to overload resolution the built in printf function is selected over your custom defined printf function. This is because the string literal "Hello" and "World" decays to const char* due to type decay and the built in printf function is a better match than your custom defined printf.
To solve this, replace the printf calls with :
printf(std::string("Hello"));
printf(std::string("World"));
In the above statements, we're explicitly using std::string's constructor to create std::string objects from the string literal "Hello" and "World" and then passing those std::string objects by value to your printf function.
Another alternative is to put your custom printf inside a custom namespace. Or you can name your function other than printf itself.
It's using the inbuilt printf method.
Try to explicitly use std::string so that it'll call custom printf method.
printf(std::string("Hello"));
printf(std::string("World"));
Or you can put your method in a different namespace:
#include <iostream>
namespace test
{
extern void printf(const std::string& message);
}
int main()
{
std::cout << "Hello" << std::endl;
std::cout << "World" << std::endl;
test::printf("Hello");
test::printf("World");
return 0;
}
void test::printf(const std::string& message) {
std::cout << message << std::endl;
}
try renaming the "printf" function to "print" it works fine-
#include <iostream>
using namespace std;
void print(string message);
int main()
{
cout << "Hello" << endl;
cout << "World" << endl;
print("Hello");
print("World");
cout <<endl;
return 0;
}
void print(std::string message) {
cout << message << endl;
}
You should pick function name other than printf(); like Print().
I am a beginner in the C++ language and have run into a compiling error that has stumped me for days for my latest assignment.
The code is using a class GradeBook to set both the name of the course and the instructor's name.
Here is the driver file where I include the header file that contains the class GradeBook
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
gradeBook gradeBook1 ();
gradeBook gradeBook2 ();
cout << "Grade Book Initial Course Name: \n" << gradeBook1.getCourseName() << endl;
cout << "Initial Instructor Name: \n" << gradeBook2.getInstuctorName() << endl;
gradeBook.displayMessage();
return 0;
}
Here is the header file. I added in the #ifndef to prevent possible looping errors based on research into this problem.
#ifndef GRADE_H
#define GRADE_H
#include <string>
#include "GradeBook.cpp"
// __Grade_Book__
class GradeBook
{
std::string courseName, instructorName;
public:
// constructor initializes courseName with string supplied an argument
GradeBook( std::string name);
//function sets the course name and limits length
void setCourseName(std::string name);
void setInstructorName(std::string iName);
//function gets the course name
std::string getCourseName();
std::string getInstructorName();
//function displays a welcome message
void displayMessage();
};
#endif
This is the source file where the error " 'GradeBook' does not name a type' occurs at the first instance of GradeBook despite being linked to the header file. I feel as though this file is not completely necessary for this code but it is required for the assignment. I'm wondering if I have messed up with the communication of the files creating a loop but everything I have tried has led to the same compiling error, insisting that the class GraddeBook has not been defined.
#include <iostream>
#include "GradeBook.h"
using namespace std;
// constructor initializes courseName with string supplied an argument
GradeBook::GradeBook(string name)
{
setCourseName(name);
setInstructorName (iName);
// call set function to initialize courseName
}
//function sets the course name
void GradeBook::setCourseName(string name)
{
if (name.length() <= 30)
courseName = name;
else
{
courseName = name.substr(0, 30); //start at 0, length of 30
cout << "Course Name \" " << name << "\" exceeds maximum length (30).\n"
<< "Limiting courseName to the first 30-characters.\n" << endl;
}
}
void GradeBook::setInstuctorName(string iName)
{
if (iName.length() <= 20)
instructorName = iName;
else
{
instructorName = iName.substr(0, 30); //start at 0, length of 30
cout << "Instructor Name \" " << iName << "\" exceeds maximum length (20).\n"
<< "Limiting Instructor Name to the first 20-characters.\n" << endl;
}
}
//function gets the course name
string GradeBook::getCourseName()
{
return courseName;
}
string GradeBook::getInstructorName()
{
return InstructorName;
}
//function displays a welcome message
void GradeBook::displayMessage()
{
cout << "Welcome to the Grade book for: \n" << getCourseName() << endl;
cout << "This course is presented by: \n" << getInstructorName() << endl;
}
Be sure to use the correct classname when declaring a variable. Identifiers are case sensitive.
GradeBook gradeBook1;
GradeBook gradeBook2;
I'm learning C++, and I'm just messing around with putting classes in separate files for practice. I have a getter function, which returns a string (because the variable is saved as a string). However, from my main() function, I am not sure how to call it. I know the problem is probably that I need to include string somewhere when I call the object, but I have no idea how to format it.
I know this is a pretty newbie questions, but I couldn't find the answer anywhere. Could someone help me out?
(p.s. I'm not trying to get this specific code to work, since it's useless. I'm just trying to learn how to apply it for future reference).
I've tried throwing in string in a couple of places when calling or creating the object, but I always get an error. I know I could get around it by not encapsulating the variable or not having a separate class file, but that's not what I want.
main.cpp
#include <iostream>
#include "usernameclass.h"
#include <string>
using namespace std;
int main()
{
usernameclass usernameobject;
usernameobject.getUsername();
return 0;
}
usernameclass.h
#ifndef USERNAMECLASS_H
#define USERNAMECLASS_H
#include <string>
class usernameclass
{
public:
usernameclass();
std::string getUsername();
void setUsername(std::string name);
askUsername();
private:
std::string usernameVar = "test";
};
#endif
usernameclass.cpp
#include "usernameclass.h"
#include <iostream>
#include "username.h"
#include <string>
using namespace std;
string usernameclass::getUsername(){
return usernameVar;
cout << "test cout" << endl;
}
usernameclass::askUsername(){
string name;
cout << "What is your name?" << endl;
cin >> name;
setUsername(name);
cout << "Ah, so your name is "+usernameVar+", great name I guess!" << endl;
cin.get();
cin.get();
cout << "You're about to do some stuff, so get ready!" << endl;
}
usernameclass::usernameclass(){}
void usernameclass::setUsername(string name){
string* nameptr = &usernameVar;
*nameptr = name;
}
Expected result: runs getUsername() function and returns usernameVar
Actual result: doesn't run the getUsername() function
The current code would not compile, because you have not specified return type of 'askUsername()' routine, which is 'void', I believe.
Other things are good, apart from an output in 'getUsername()', which happens after returning from the function and about which you should have received a warning, I guess.
To the question: you can call that 'get' method in 'main()' as:
cout << usernameobject.getUsername();
Your code should be structured more like this instead:
main.cpp
#include <iostream>
#include "usernameclass.h"
int main()
{
usernameclass usernameobject;
// optional:
// usernameobject.askUsername();
// do something with usernameobject.getUsername() as needed...
return 0;
}
usernameclass.h
#ifndef USERNAMECLASS_H
#define USERNAMECLASS_H
#include <string>
class usernameclass
{
public:
std::string getUsername() const;
void setUsername(std::string name);
void askUsername();
private:
std::string usernameVar = "test";
};
#endif
usernameclass.cpp
#include <iostream>
#include "usernameclass.h"
std::string usernameclass::getUsername() const {
return usernameVar;
}
void usernameclass::setUsername(std::string name) {
usernameVar = name;
}
void usernameclass::askUsername() {
std::string name;
std::cout << "What is your name?" << std::endl;
std::getline(std::cin, std::name);
setUsername(name);
std::cout << "Ah, so your name is " << getUsername() << ", great name I guess!" << std::endl;
std::cout << "You're about to do some stuff, so get ready!" << std::endl;
}
I am learning about functions and classes, and wrote my own code. I used the constructor to just initialize the variables. I have a function that is supposed to get the info I initialized with the constructor and allow me to display it. However, it doesn't want to work. I am not really sure what I am doing wrong. My error code says that I have unresolved externals because of my "void" function. I thought my function was not returning anything but rather just displaying the input it got from the initialization of the constructor.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Berries {
string Nameofberries;
int Price;
public:
Berries (string N,int B)
{
Nameofberries = N;
Price = B;
}
void GetBerryInfo(const Berries& B)
{
cout << B.Nameofberries << endl;
cout << B.Price << endl;
}
};
void GetBerryInfo (const Berries& B);
int main ()
{
Berries Berryinfo1( "Raspberries", 7);
cout << GetBerryInfo;
system("pause");
return 0;
}
There are several mistakes.
void GetBerryInfo(const Berries& B)
{
cout << B.Nameofberries << endl;
cout << B.Price << endl;
}
should be
void GetBerryInfo()
{
cout << Nameofberries << endl;
cout << Price << endl;
}
==================================================================
void GetBerryInfo (const Berries& B);
should be removed.
==================================================================
cout << GetBerryInfo;
should be
Berryinfo1.GetBerryInfo();
==================================================================
All computer langauges are fussy, you have to get the details right, as well as understand the concepts.
This will do what you wanted:
# include <iostream>
# include <iomanip>
# include <string>
using namespace std;
class Berries {
string Nameofberries;
int Price;
public:
Berries (string N,int B)
{
Nameofberries = N;
Price = B;
}
void GetBerryInfo()
{
cout << Nameofberries << endl;
cout << Price << endl;
}
};
int main ()
{
Berries Berryinfo1( "Raspberries", 7);
Berryinfo1.GetBerryInfo();
system("pause");
return 0;
}
A couple of points on your mistakes:
GetBerryInfo() was declared inside the class. You don't need to re-declare it in the global scope. That 2nd declaration should be removed.
To be invoked, functions (like GetBerryInfo) must have () at the end of them like so: GetBerryInfo().
There is no point for GetBerryInfo() to take Berries as a paremeter. It is a member function that is part of the class Berries. It has access to all data members of a Berries instance already.
You don't need to use cout here: cout << GetBerryInfo; because the function body already sends the data members to cout. This function returns void so it doesn't make sense to send this to cout anyway.
#include <iostream>
#include <string>
using namespace std;
// my code starts
class Cat {
public:
int age;
string name, race, voice;
Cat(int age2,string name2,string race2,string voice2);
void PrintInformation();
};
Cat::Cat(int age2,string name2,string race2,string voice2) {
age = age2;
name = name2;
race = race2;
voice = voice2;
}
Cat::Meow(){
cout << "Cat says: " << fluffy.Meow() << endl;
}
void Cat::PrintInformation() {
cout << "Name: " << name;
cout << "\nAge: " << age;
cout << "\nRace: " << race << endl;
}
// my code ends
int main()
{
Cat fluffy(2, "Fluffy", "Bombay", "Meoow!!!");
fluffy.PrintInformation();
cout << "Cat says: " << fluffy.Meow();
}
I can't seem to figure out how to make this code work. My main problems seems to be that i don't know how to call fluffy.Meow(); from int main().
Thanks, for any help!
You forgot to declare Cat::Meow in the class declaration.
//some code
void PrintInformation();
void Meow();
Additionally, you have to specify what the return type of the function Meow is, in your case it would be void, because it returns nothing.
You also have some recursion going on, Meow calling Meow (forgetting about the fact that fluffy isn't a variable in this scope). Your Cat class knows nothing about the instance fluffy, so you can't access it.
I guess you meant voice instead.