How to output Classes and Objects [duplicate] - c++

This question already has answers here:
How to print class object using operator<<
(3 answers)
Closed 3 months ago.
#include <iostream>
using namespace std;
class PrintName{
public:
void studentName(){
cout<<"Name : "<<studentName<<endl;
}
};
class MathClass{
public:
void multiplicationFunc(int x, int y){
cout<<"Result : "<<(x*y)<<endl;
}
};
int main()
{
cout<<endl;
PrintName PN;
PN.studentName("Mark", "Santo");
MathClass MC;
MC.multiplicationFunc(10,5);
}
I am new to C++ and am learning about classes and objects. From what I gathered classes are ways to group functions and objects is the ability to access them? I am having trouble getting this code to work, I receive an error on line 15 for 'error: no match for 'operator<<'. I am trying to fix my class in order for the main function to work. The output should simply be
'Name : Mark Santo'
'Result : 50'
Thank you for your help everyone!

It seems like there is no error on line 15. Instead, you did not define the variable studentName in your void studentName() function in line 8. Also, you used 2 arguments for studentName() in line 23, where the original function is not taking any. This is the corrected code:
#include <iostream>
using namespace std;
class PrintName{
public:
void studentName(string x, string y){
cout<<"Name: " << x << " " << y << endl;
}
};
class MathClass{
public:
void multiplicationFunc(int x, int y){
cout<<"Result: " << x*y << endl;
}
};
int main()
{
cout << endl;
PrintName PN;
PN.studentName("Mark", "Santo");
MathClass MC;
MC.multiplicationFunc(10,5);
}

Related

Error reference to 'distance' is ambiguous [duplicate]

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 2 years ago.
I cannot figure out why I am getting the error "reference to 'distance' is ambiguous".
I have passed class object as an argument in friend function.
#include <iostream>
using namespace std;
class distance {
int meters = 0;
public:
distance() {}
void displaydata() {
cout << "Meters Value:" << meters;
}
//Prototype
friend void addvalue(distance &d);
};
void addvalue(distance &d) {
d.meters += 5;
}
int main() {
distance d1; // meters = 0
d1.displaydata(); // 0
// The friend function call
addvalue(d1); // pass by reference
d1.displaydata();
}
If you remove the using namespace std; and change cout to std::cout, then your code compiles w/o error.
Discovering the source of the ambiguity is left as an exercise to the reader

constructor's problem to initialize pointer data member

I encountered a problem while initializing a pointer data member i.e int* apex; inside a constructor
having parameter as int i = 0; as *apex = i;
but unfortunately nothing is executed after compiler strikes this line.
#include <iostream>
using namespace std;
class base{
int *apex;
public:
explicit base(int i = 0){
cout << "this does executes" << endl;
*apex = i; // <<<<<--- problem???
cout << "this doesnt executes" << endl;
}
};
int main(void){
base test_object(7);
cout << "this also doesnt executes";
}
// I know how to avoid this but i want to know what
// exactly the problem is associated with *apex = i;
THANKS IN ADVANCE
note-no error is generated
What you wrote is equivalent to:
int *apex;
*apex = 42;
which is undefined behavior (UB), which includes that the compiler might just include code to stop execution or to start playing the song Never Gonna Give You Up by Rick Astley.
Even
int *apex = nullptr;
*apex = 42;
would be UB because the int* pointer has to point to a valid int when dereferencing via *
Just write
class base{
int apex{};
public:
explicit base(int i) : apex(i){}
};
And be done for
I got it. Trust me I am ashamed of myself after this silly doubt.
#include <iostream>
using namespace std;
class base{
int *apex;
public:
explicit base(int i = 0){
apex = new int;
// this is what i was supposed to do
*apex = i;
}
};
int main(void){
base test_object(7);
}
Your pointer points to invalid address you didn't initialize it
This will fix what you have asked to be done.
using namespace std;
class base{
int *apex{nullptr};
public:
explicit base(int& i ): apex{&i} {
cout << "this does executes" << endl;
cout << "this doesnt executes" << endl;
}
};
int main(void){
int a = 7
base test_object(a);
cout << "this also doesnt executes";
}
Make sure something (int) given to ctor has longer lifetime than an instance.

creating a simple class and its variables and function keeps returning " segmentation fault (core dumped)", C++

I am new to c++. Ive been trying to make a class, assign private variables to it and make public funcs to modify and reach those variables later.
Program keeps returning "Segmentation fault (core dumped) Process returned 139 (0x8B).
I know there are similar topics with this error, but not I could not find any suitable solution there. Thank you for your time.
# include <iostream>
# include <string>
using namespace std;
class cars{
public:
string yearinsert(int x) {
year = x;
}
string nameinsert(string y) {
name = y;
}
void nameget() {
cout << name << endl;
}
void yearget() {
cout << year << endl;
}
private:
int year;
private:
string name;
};
int main(){
cars skoda;
skoda.nameinsert("Skoda");
skoda.yearinsert(2012);
skoda.nameget();
skoda.yearget();
return 0;
};
Most probably, the problem is solved as soon as your functions return values. This should be a compiler warning, but it is not by default.

Why class defination after main() function will not work? [duplicate]

This question already has answers here:
C++ what to code if i put a class after main() function
(5 answers)
Closed 7 years ago.
If the function is defined after main() function program will work...
void printdata(int i);
int main()
{
printdata(20);
return 0;
}
void printdata(int i)
{
std::cout << "i = " << i << std::endl;
}
If we declare class before main function and defined after main function, why it will through error?
#include <iostream>
class C;
int main()
{
C c(20);
c.printdata();
return 0;
}
class C
{
int i;
public:
C(int a) : i(a) {};
void printdata()
{
std::cout << "C:i = " << i << std::endl;
}
};
Error log after compiling the code:
class_after_main.cpp: In function ‘int main()’:
class_after_main.cpp:7:6: error: variable ‘C c’ has initializer but incomplete type
C c(20);
If you declare a class without defining it, you can only use reference or pointer to that class. The compiler needs to know the size of the class to define the size needed to store its objects.

Static Functions and class variables?

I have broken down my issue into a small simple program.
I have a class myclass I have created in a separate .cpp file "classes.cpp" and declared in the header file "classes.h". myclass contains a variable a of which is initialized when instantiated. This makes variable a = 5.
My overall goal is to create a class in a separate .cpp file declared in a .h file which I can create multiple instances of in my main() program. The problem I am having is this.
In my main() function I create an instance of myclass called first.
my main program shows the variable a is set to the number 5.
If I want to change that number using a static function (and it has to be a static function as this relates to something much bigger in another program I am writing). I call the static function directly and in that static_function I create an instance of myclass and call the non_static_function because static functions have no implicit 'this' connecting them to an object.
In my non_static_function I change the value to the number 8. The problem is that the value of variable 'a' in 'first' remains at 5 when I want it to be 8. I need to change the value using first->static_function(8) and not by first->a = 8. . How can I do this?
Code below:
**main.cpp**
#include <iostream>
#include "classes.h"
using namespace std;
int main()
{
myclass *first = new myclass();
cout << "Myclass variable a is = " << first->a << endl;
first->static_function(8); // trying to change myclass variable 'a' to 8.
cout << "But" << endl;
cout << "the actual value of a is still: " << first->a << endl;
}
**classes.h**
#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED
class myclass
{
public:
int a;
myclass();
void non_static_function(int x);
static void static_function(int x);
};
#endif // CLASSES_H_INCLUDED
**classes.cpp**
#include <iostream>
#include <cstdlib>
#include "classes.h"
using namespace std;
myclass::myclass()
{
a = 5;
}
void myclass::non_static_function(int x)
{
a = x;
cout << "The value for variable 'a' was 5 but is now: " << a << endl;
}
void myclass::static_function(int x)
{
myclass *p = new myclass();
p->non_static_function(x);
}
If you want every instance of myclass to have its own a and you want to call a static function to change it then you need to pass the instance you want changed to the static function. A static function can only modify static members of a class or the members of an instance that is inside its scope. Non static member functions can change any variable that is a member of the class.
class Foo
{
private:
int bar;
public:
static void static_function(int value, Foo & foo) { foo.bar = value; }
void non_static_function(int value) { bar = value; }
};
int main()
{
Foo foo;
Foo::static_function(8, foo);
// now bar will have the value of 8
foo.non_static_function(20);
// now bar will have the value of 20
}
I have finally found a way to deal with this small problem. Above the 'myclass' definition in classes.cpp I declare a 'myclass' variable
myclass *tgt; . Then in my constructor for 'myclass' I just allocate the instantiated object to a my global myclass variable of which I can access from the myclass definition tgt = this; Now I can use tgt in my static function to call the non_static_function in my 'myclass' definition and it all works perfectly.
NathanOliver, you are correct in saying that I need a class instance but the way I have done it here suits my needs. Passing the instance of myclass is certainly another way of doing this but it would require a global function above my 'myclass' definition.
Thanks for the help.
**main.cpp**
#include <iostream>
#include "classes.h"
using namespace std;
int main()
{
myclass *first = new myclass();
cout << "Myclass variable a is = " << first->a << endl;
first->non_static_function(8); // trying to change myclass variable 'a' to 8.
cout << "But" << endl;
cout << "The actual value of a is still: " << first->a << endl;
myclass *second = new myclass();
cout << "For the 'second' class the variable a is: " << second->a << endl;
second->non_static_function(23);
cout << "After calling the static function from 'second' the value of a is: " << second->a << endl;
cout << "And first->a is still: " << first->a << endl;
}
**classes.h**
#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED
class myclass
{
public:
int a;
myclass();
void non_static_function(int x);
static void static_function(int x);
};
#endif // CLASSES_H_INCLUDED
**classes.cpp**
#include <iostream>
#include <cstdlib>
#include "classes.h"
using namespace std;
myclass *tgt; // *Add a global myclass variable above the myclass
definition*
myclass::myclass()
{
tgt = this; // *In the constructor allocate the instantiated class
//from main() to "tgt" .*
a = 5;
}
void myclass::non_static_function(int x)
{
a = x;
// Now see that the value of a is changed.
cout << "The value for variable 'a' was 5 but is now: "<< this->a << endl;
}
void myclass::static_function(int x)
{
tgt->non_static_function(x);
}