I have an assignment for my OOP class to create a rudimentary calculator. The goal of the assignment is to practice namespaces and exception handling.
I will paste the text of the problem for clarity:
You have to implement the class Kalkulator which solves rudimentary mathematical expressions and returns the result as an integer.
The class Kalkulator should be put in a namespace called Matematika. The whole class needs to be implemented in the header file without a .cpp file.
You have to define two exceptions and throw them in the correct situation(they are caught in the main function)
IncorrectDataExc put the class into the namespace Matematika->Exceptions
InvalidOperationExc put the class into the namespace Matematika->Exceptions
and they also gave me the main function:
#include <iostream>
#include <string>
#include "Kalkulator.h"
#include "IncorrectDataExc.h"
#include "InvalidOperationExc.h"
using namespace std;
int main()
{
string s;
getline(cin, s);
try
{
Matematika::Kalkulator kalkulator;
cout << kalkulator.izracunaj(s) << endl;
}
catch(Matematika::Exceptions::IncorrectDataExc)
{
cout << "Exception IncorrectData" << endl;
}
catch(Matematika::Exceptions::InvalidOperationExc)
{
cout << "Exception InvalidOperation" << endl;
}
return 0;
}
The problem I'm having is that i don't know what to throw when i get an exception and i don't know how the InvalidOperationExc and IncorrectDataExc classes should look like because i don't know what the catch functions are actually catching(an object?)
I have to create 3 header files:
IncorrectDataExc.h
InvalidOperationExc.h
Kalkulator.h
This is what i have so far
#pragma once
#include <string>
#include <sstream>
using namespace std;
namespace Matematika{
class Kalkulator{
private:
public:
int izracunaj(string mojString)
{
int prviBroj = 0;
int drugiBroj = 0;
char operacija = ' ';
stringstream ss(mojString);
ss >> prviBroj >> operacija >> drugiBroj;
if(operacija != '*' && operacija != '+' && operacija != '/' && operacija != '-')
{
Matematika::Exceptions::InvalidOperationExc greska1;
throw greska1;
}
if(prviBroj % drugiBroj != 0)
{
Matematika::Exceptions::IncorrectDataExc greska2;
throw greska2;
}
}
};
}
and I'm getting some errors:
'Exceptions': the symbol to the left of a '::' must be a type
'InvalidOperationExc' : is not a member of 'Matematika'
'InvalidOperationExc' : undeclared identifier
missing ';' before identifier 'greska1'
'greska1' : undeclared identifier
'greska1' : undeclared identifier
I don't know what I'm doing wrong. The part that is confusing me the most is this
catch(Matematika::Exceptions::IncorrectDataExc)
what is the catch function receiving?
Pretty self explanatory errors:
'InvalidOperationExc' : is not a member of 'Matematika'
In other words, it doesn't exist. This
Matematika::Exceptions::InvalidOperationExc greska1;
resulted in the error. You should either change the InvalidOperationExc to something else or create it in Matematika.
int izrakunaj() doesn't need Matematika::... to access members of namespace Matematika because it is also a member.
namespace ns
{
int integer; //ns::integer
void set0(){integer=0;} //modifies the variable above
void set1(){ns::integer=1;} //modifies the variable below
namespace ns
{
int integer; //ns::ns::integer
}
}
Related
I am trying to place objects in an array list and 3 errors popped up. I looked into the forums and there's a question similar to mine but I don't think it's applicable in my case.
Here's my code:
in test.cpp (main file)
#include <iostream>
#include "House.h"
using namespace std;
House HouseArray[2];
int main()
{
string toPrint;
House Kubo("Kubo", 2);
HouseArray[0] = Kubo;
toPrint = HouseArray[0].GetHouseName;
cout <<toPrint<< endl;
}
in House.cpp
#include "House.h"
#include <iostream>
House::House(string a, int h)
{
Name = a;
Health = h;
}
void House::DamageHouse(int d) {
Health -= d;
cout << "Your " << Name << " has " << Health << " left."<<endl;
}
int House::GetHouseHealth() {
return Health;
}
string House::GetHouseName() {
string returning = Name;
return returning;
}
House::~House()
{
}
in House.h
#include <string>
using namespace std;
class House
{
string Name;
int Health;
public:
House(string a, int h);
int GetHouseHealth();
void DamageHouse(int d);
string GetHouseName();
~House();
};
Errors:
Error C2512 'House': no appropriate default constructor available test.cpp in line 9
Error C3867 'House::GetHouseName': non-standard syntax; use '&' to
create a pointer to
member test.cpp in line 16
Error C2679 binary '=': no operator found which takes a right-hand
operand of type 'overloaded-function' (or there is no acceptable
conversion) test.cpp in line 16
You need a default constructor if you want to create an array like that: House HouseArray[2]; The compiler will need to know how to create an empty House so that the initial array can be initialised. Thus add something like the following to House.h
House() {
Name = "";
Health = 0;
}
To call a function on a class, you need to add the braces:
toPrint = HouseArray[0].GetHouseName();
I suspect that the above will solve this issue as well.
I am fairly new to c++ (java background) and I'm trying to access a member of a class I have created but I keep getting an error message when trying to call a member of a class, it's saying that the variable is not a member of the class.
Any ideas why this is happening? I've looked at so many other examples of people with this problem, but none of them have helped me find out why
Main.cpp
#include "stdafx.h"
#include "Adressbuch.h"
#include "Kontakt.h"
#include <iostream>
#include <sstream>
using namespace std;
Adressbuch hinzufügen(Adressbuch buch);
Adressbuch löschen(Adressbuch buch);
void auflisten(Adressbuch buch);
int main()
{
bool end = true;
Adressbuch buch;
while (end) {
cout << "Bitte geben sie ein Aktion ein: (hinzufügen(h)/löschen(l)/beenden(b)/auflisten(a))"
<< endl << "zur Zeit gibt es " << buch.adress_buch.size() << " Kontakte" << endl;
if (cin >> "h") buch = hinzufügen(buch);
else if (cin >> "l") buch = löschen(buch);
else if (cin >> "a") auflisten(buch);
else if (cin >> "b") end = true;
else cout << "Error. Ungultig Eingabe." << endl;
}
return 0;
Adressbuch.h
#include "Kontakt.h"
#include <list>
class Adressbuch{
public:
Adressbuch();
~Adressbuch();
void hinzufügen(Kontakt k);
void löschen(Kontakt k);
list<Kontakt> Adressbuch::adress_buch;
};
Adressbuch.cpp
#include "Adressbuch.h"
#include "Kontakt.h"
#include <list>
using namespace std;
Adressbuch::Adressbuch(){
adress_buch;
}
Adressbuch::~Adressbuch(){
}
void Adressbuch::hinzufügen(Kontakt k){
adress_buch.push_back(k);
}
void Adressbuch::löschen(Kontakt k) {
adress_buch.remove(k);
}
The member that I am having trouble with, is the list adress_buch. Anytime I try to call it, it says its not a member, even though it is defined in the header class?
Error message on line 19 of main()
Severity Code Description Project File Line Suppression State
Error C2039 'adress_buch': is not a member of 'Adressbuch'
ConsoleApplication5 c:\users\gregs\documents\visual studio
2015\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp 19
First a minimal, complete verifiable example that contains nothing but the code required to trigger the error:
#include <list>
using namespace std;
class Adressbuch
{
public:
list<int> Adressbuch::adress_buch;
};
int main()
{
Adressbuch buch;
buch.adress_buch.size();
return 0;
}
That's all that's needed, little bit more than, to find the problem. With nothing else in the way as a distraction problem 1 is easy to spot. I'm no guru in the Visual Studio compiler and I don't have one available, but I'm betting that somewhere in the warnings or errors is this line:
list<int> Adressbuch::adress_buch;
adress_buch is improperly defined causing all sorts of future problems. Compiling this example, GCC gives:
error: extra qualification 'Adressbuch::' on member 'adress_buch'
A corrected example is
#include <list>
using namespace std;
class Adressbuch
{
public:
list<int> adress_buch;
};
int main()
{
Adressbuch buch;
buch.adress_buch.size();
return 0;
}
Or better
#include <list>
class Adressbuch
{
public:
std::list<int> adress_buch;
};
int main()
{
Adressbuch buch;
buch.adress_buch.size();
return 0;
}
Because the notorious, bug-hiding using namespace std; has been removed
I am trying to cout some variables but the compiler says that cout is undefined. I have included iostream and am using namespace std. Removing using namespace std and using std::cout instead changes the issue to "namespace "std" has no member "cout" ". I found some answers saying to add # include "stdafx.h" to the code but Error: cannot open source file "stdafx.h" occurs.
Code is:
#include "Complex.h"
#include <cmath>
#include <iostream>
using namespace std;
Complex::Complex(int PolarOrRectang, float RealOrArg, float ImagOrAng) {
if (PolarOrRectang == 0) {
real = RealOrArg;
imag = ImagOrAng;
else {
real = RealOrArg * cos(ImagOrAng);
imag = RealOrArg * sin(ImagOrAng);
}
};
void Complex::getValue(int PolarOrRectang) {
if (PolarOrRectang == 0) {
cout << real << " +_" << imag << "i" << endl;
} else {
cout << sqrt((real^2) + (imag^2)) << "*e^-" << atan(imag / real)<< endl;
}
};
I'm trying to define a class, so my main is elsewhere.
Running a very basic program that just couts "hello world" works fine, the problem is specific to this code.
Put #include<iostream> at the first position, the order is important
#include "Complex.h"
#include <iostream>
#include <cmath>
PS: Why do you use std:: when you are using "using namespace std;"?
Following is my code
#include "stdafx.h"
#include <iostream>
int main()
{
using namespace std;
int x;
cout << "enter value one";
cin >>x;
cout << endl;
//cout << x;
do_print(x);
return 0;
}
int do_print(int n)
{
using namespace std;
cout << "\n I am great \n";
square(n);
return 0;
}
int square(int n)
{
using namespace std;
int y = n*n;
cout << y;
cout << endl;
return 0;
}
and I am getting error.
Error 1 error C3861: 'do_print': identifier not found
Error 2 error C3861: 'square': identifier not found
I am using Visual C++ 2013. please tell me if there is something I am missing.
In unction do_print you are using identifier square
square(n);
But in the point where the identifier is used it was not yet defined. So the compiler issues an error because it doen not know the definition of square,.
Place the identifier definition before its using either in function do_print or before main or before do_print
The same is valid for identifier do_print itself. Its declaration shall be before its using.
For example write the following way
int do_print( int );
int square( int );
int main()
{
Header file
#include <iostream>
#include <cstring>
const unsigned MaxLength = 11;
class Phone {
public:
Phone(const char *phone) {
setPhone(phone);
}
void setPhone(const char Phone[ ]);
const char* getPhone();
private:
char phone[MaxLength+1];
};
Cpp file
#include "Phone.h"
#include <iostream>
#include <ctype.h>
#include <cstring>
using namespace std;
bool checkNum(char num[]);
void Phone::setPhone(const char Phone[ ]) {
strncpy(phone, Phone, MaxLength);
phone[MaxLength] = '\0';
}
const char* Phone::getPhone() {
return phone;
}
int main() {
Phone i1("12345678901");
cout << i1.getPhone() << endl;
if (checkNum(i1.getPhone))
cout << "Correct" << endl;
else
cout << "Invalid Wrong" << endl;
}
bool checkNum(char num[]) {
bool flag = true;
if (isdigit(num[0]) == 0)
flag = false;
return flag;
}
When I tried to compile, I get this error:
error C3867: 'Phone::getPhone':
function call missing argument list;
use '&Phone::getPhone' to create a
pointer to member
I'm getting an error on this line "if (checkNum(i1.getPhone))". I created a Phone object and what I am trying to do is use the function checkNum to see if the first index of the array is a number. Am I referencing the object wrong? Should I use indirect selection operator instead? Any idea what I am doing wrong?
Thanks
You are missing a pair of parentheses after getPhone in if (checkNum(i1.getPhone)); it should be if (checkNum(i1.getPhone())).
The line:
if (checkNum(i1.getPhone))
should be
if (checkNum(i1.getPhone()))