Error for pointer to member in C++ - c++

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()))

Related

Use of undeclared identifiers even though class is created in c++

I have created a class ShowTicket coded as follows in a header file:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class ShowTicket {
public:
//function that returns true if sold status is true and false if it doesnt.
bool is_sold(void){
if (sold_status == true){
return true;
}
else{
return false;
}
}
//function that sets sold_status to true
void sell_seat(void){
sold_status = true;
}
//prints row, seat number and sold status in casual terms
string print_ticket(void){
ostringstream sout;
if(sold_status == true){
sout<<row<<" "<<seat_number<<"sold";
}
else{
sout<<row<<" "<<seat_number<<"available";
}
return sout.str();
}
//initilizes variables in constructor
bool sold_status;
const char* row;
const char* seat_number;
//constructor
ShowTicket(const char* row, const char* seat_number, bool sold_status){
sold_status = false;
}
};
I am using a main file with the following code to test this class
#include <iostream>
#include <string>
#include <sstream>
#include "showticket.h"
using namespace std;
int main () {
ShowTicket myticket1(“AA”,”101");
ShowTicket myticket2(“AA”,”102”);
if(!myticket1.is_sold())
myticket1.sell_seat ();
cout << myticket1.print_ticket() << endl;
cout << myticket2.print_ticket() << endl;
return 0;
}
I am receiving multiple "Use of undeclared identifier" errors and "Non-ASCII characters are not allowed outside of literals" errors and I do not know how to fix them.
Any help is greatly appreciated.

Custom exception handling can not run in Ubuntu but Dev C can

When i use this code bellow: exception error is not print out screen when I using g++ in Ubuntu but when i change to Dev C in C++.
#include <iostream>
#include <exception>
#include <stdexcept>
#include <string.h>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//Define Class for exception error
class NoBinaryNumber: public logic_error{
private:
string s;
int x;
public:
NoBinaryNumber(string msg,int x) : logic_error(msg){
s = msg; this->x=x;
}
const char* what() const throw(){
ostringstream a;
a.str("");
a << s << " is wrong at position "<< x;
if(a)
return a.str().c_str();
else
return "";
}
~NoBinaryNumber() throw(){};
};
class BinaryStringToNumber{
public:
int number;
BinaryStringToNumber(string s){
number=0;
for (int i=s.length()-1;i>=0;i--){
if ((s[i]=='1')||(s[i]=='0')){
number=number+(s[i]-'0')*pow(2,i);
}
else{
throw NoBinaryNumber(s,s.length()-i);
cout << "alala" ;
}
}
}
};
int main(){
//Using customer exception error
try{
BinaryStringToNumber a("2");
cout << a.number << endl;
}
catch (NoBinaryNumber& e){
cout << e.what() << endl;
}
return 0;
}
The result shoud be: "2 is wrong at position 1"
In your what() function, you are creating an object (ostringstream), which is destructed at the end of this function call, and trying to access its content (a.str().c_str()) outside of what() function. This is an undefined behaviour (UB) and the fact that DevC++ is showing is just because it is an UB.
To solve this, I suggest you to create your message at constructor call and use what() just to show this message. Something like this:
class NoBinaryNumber: public logic_error{
private:
string s;
int x;
public:
NoBinaryNumber(string msg,int x) : logic_error(msg){
s = msg + " is wrong at position " << std::to_string(x);
this->x=x;
}
const char* what() const throw(){
return msg.c_str();
}
};

Convert char to const char* String Insert C++

I have a problem with String Insertion because, I can not add a char, just a const char. How can i easily convert it?
The compiler just accept like this:
b.insert(i,"a");
But i want like this:
b.insert(i,b[ii]);
Full Code:
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string a,b;
int aa=0;
cin >> a;
b=a;
for(int i=0;i<a.length()+1;i++)
{
for(int ii=0;ii<a.length();ii++)
{
b.insert(i,a[ii]);
if (b == string(b.rbegin(), b.rend()))
{
cout << b << endl;aa=1;
break;
}
b.erase (b.begin()+i);
}
if(aa=1)break;
}
if(aa==0)
cout << "NA" << endl;
return 0;
}
See the documentation for std::string::insert. The version that takes a single char also needs a count argument.
b.insert(i,1,b[ii]);
You should use the following overload of std::string::insert:
basic_string& insert( size_type index, size_type count, CharT ch );
See http://en.cppreference.com/w/cpp/string/basic_string/insert

ERROR deprecated conversion from string constant to 'char*'

I'm stuck with error message deprecated conversion from string constant to 'char*'
What I tried to do here is to assign "First", "Last" to cfoo1 and make cfoo2 equal to cfoo1. Lastly, display cfoo1 and cfoo2 to standard output.
#include <iostream>
#include <cstring>
#include "cfoo.h"
using namespace std;
CFoo :: CFoo(char first[], char last[]){
m_first[BUF] = first[BUF];
m_last[BUF] = last[BUF];
}
void CFoo :: WriteFoo(){
cout << m_first[BUF] << ", " << m_last[BUF];
}
#ifndef CFOO_HEADER
#define CFOO_HEADER
#include <iostream>
#include <cstring>
using namespace std;
const int BUF = 256;
class CFoo{
public:
CFoo(char first[], char last[]);
void WriteFoo();
private:
char m_first[BUF];
char m_last[BUF];
};
#endif
#include <iostream>
#include "cfoo.h"
using namespace std;
int main(){
CFoo foo1("Jong", "Yoon");
CFoo foo2 = foo1;
cout << "foo1 = ";
foo1.WriteFoo();
cout << endl;
cout << "foo 2 = ";
foo2.WriteFoo();
cout << endl;
return 0;
}
There are two issues:
Using string literals (which are of type char const*) to call a function that expects char[].
Trying to assign to char arrays.
Fixes:
Change the constructor to:
CFoo(char const* first, char const* last);
Change its implementation to:
CFoo(char const* first, char const* last)
{
// Make sure to copy at most BUF-1 characters
// to m_first and m_last.
m_first[0] = '\0'
strncat(m_first, first, BUF-1);
m_last[0] = '\0'
strncat(m_last, last, BUF-1);
}
You also need to change the implementation of CFoo::WriteFoo() to use the entire string
void CFoo::WriteFoo()
{
cout << m_first << ", " << m_last;
}
Also,
Accessing m_first[BUF] or m_last[BUF] is an error since the maximum value of a valid index to access those arrays is BUF-1.

C++ : Turn char array into a string that is printed

just a beginner student learning basic C++. I'm trying to figure out the best way to:
Turn a char array Name of 20 into a string that can be printed.
I found in other Stack Overflow topics to use "str()" such as "str(Name)", but it always comes up 'identifier not found'.
cout << "Name:" << str(Name) << endl;
Set a char array of 20 characters. For some reason, the following gives me errors when declaring. I've tweaked it so many times, but I cannot get why it won't give.
TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};
Full code I have so far:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
//Step 1
struct StudentRecord
{
char Name[20];
//Accessor
void printInfo() const;
};
void StudentRecord::printInfo() const
{
cout << "Name:" << str(Name) << endl;
}
int main()
{
//Step 2
StudentRecord TESCStudent;
TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};
//Step 3
TESCStudent.printInfo();
_getch();
return 0;
}
Given that you are at a very beginner level, just use std::string:
#include <iostream>
#include <conio.h>
#include <string>
struct StudentRecord {
std::string Name;
void printInfo() const {
std::cout << "Name:" << Name << '\n';
}
};
int main() {
StudentRecord TESCStudent;
TESCStudent.Name = "SuperProgrammer";
TESCStudent.printInfo();
_getch();
}
Live demo
The syntax like this:
char Name[20] = {'S','u','p','e','r','\0'};
is used to initialize a variable when you define it. However, in your case,
StudentRecord TESCStudent;
TESCStudent.Name[20] = ...;
You've already defined it on the line before, so you can't "initialize", you have to "assign" it.
This is pretty much why you use std:string instead of char[].