3 Replies - 767 Views - Last Post: 16 April 2019 - 07:17 AM Rate Topic: ****- 4 Votes

#1 Neonz27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 08-May 16

Error: Expected Unqualified-ID

Posted 16 April 2019 - 12:46 AM

I was recently helping a student of mine learn proper memory management and class usage when I stumbled across the following error:

exit status 1
Account.cpp:42:12: error: expected unqualified-id
  Account::*balance = (Account::getBalance() + _amount);
           ^
Account.cpp:46:12: error: expected unqualified-id
  Account::*balance = (Account::getBalance() - _amount);
           ^
2 errors generated.



Account Class Source File

#include "Account.hpp"

// Constructor Method
Account::Account(std::string _name, int _checking, int _balance) {
  Account::name = &_name; 
  Account::checking = &_checking; 
  Account::balance = &_balance;
}

// Destructor Method
Account::~Account() {
  // Freeing the memory from our pointers.
  delete Account::name;
  delete Account::checking;
  delete Account::balance;

  // Declare remaining pointers as null (empty).
  Account::name = NULL;
  Account::checking = NULL;
  Account::balance = NULL;
}

 // Accessor Methods (Getters)
std::string Account::getName() {
  return *name;
}

int Account::getChecking() {
  return *checking;
}

int Account::getBalance() {
  return *balance;
}

// Public Mutator Methods (Setters)
void Account::setName(std::string _name) {
  Account::name = &_name;
}

void Account::deposit(int _amount) {
  Account::*balance = (Account::getBalance() + _amount);
}

void Account::withdraw(int _amount) {
  Account::*balance = (Account::getBalance() - _amount);
} 

// Private Mutator Method (Setter)
void Account::setChecking(int _checking) {
  Account::checking = &_checking;
} 

void Account::setBalance(int _balance) {
  Account::balance = &_balance;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error: Expected Unqualified-ID

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6881
  • View blog
  • Posts: 23,344
  • Joined: 05-May 12

Re: Error: Expected Unqualified-ID

Posted 16 April 2019 - 04:17 AM

If Account::balance were a pointer to an int, and you were trying to assign an integer to it, put the * before Account, not after the scope operator ::.

In this case, though, it's unclear why you are trying assign an integer to a pointer.

Anyway why is your Account::balance a pointer? Why is it pointing to the address of the parameter passed in on line 7 and line 54?

Also beware of using leading underscores for variable names in C and C++. There are typically reserved for use by the compiler.
Was This Post Helpful? 2
  • +
  • -

#3 Neonz27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 08-May 16

Re: Error: Expected Unqualified-ID

Posted 16 April 2019 - 06:58 AM

Thanks for pointing that out. I am using pointers for the class's member variables so they can point to the address of an existing input variable rather than creating another exact duplicate. Also I am using the underscores before the parameters to signify that the variable is temporary and will cease to exist outside of this function's scope. It's basically just a visual thing for the guy I'm teaching. Anyways thanks for the help!

This post has been edited by Skydiver: 16 April 2019 - 10:12 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5726
  • View blog
  • Posts: 17,531
  • Joined: 25-December 09

Re: Error: Expected Unqualified-ID

Posted 16 April 2019 - 07:17 AM

Just a couple of points you seem to have neglected.

1. Look a this snippet:
// Constructor Method
Account::Account(std::string _name, int _checking, int _balance) {
  Account::name = &_name; 
  Account::checking = &_checking; 
  Account::balance = &_balance;
}

You said: I am using pointers for the class's member variables so they can point to the address of an existing input variable rather than creating another exact duplicate. Do you realize that you are making copies of all of those parameters? Also do you realize that you are using a pointer to a temporary value that goes out of scope when the function ends. Trying to access those pointers after that function ends will invoke undefined behavior. In C++ you really should avoid raw pointers whenever possible. Also using a leading underscore for variables in the global scope invokes undefined behavior. I really suggest you find some other way of denoting "temporary" variables.

2. Look at this snippet:
// Destructor Method
Account::~Account() {
  // Freeing the memory from our pointers.
  delete Account::name;
  delete Account::checking;
  delete Account::balance;

  // Declare remaining pointers as null (empty).
  Account::name = NULL;
  Account::checking = NULL;
  Account::balance = NULL;
}

This class didn't allocate any memory therefore it should not be trying to delete any memory. Whoever owns the memory should be taking care of deleting the memory.

Lastly using manual memory allocation should be avoided whenever possible, if you must use dynamic memory then consider using a smart pointer instead of a "raw" pointer. And again avoid pointers whenever possible, they are dangerous and in C++ there is usually better ways of doing things than using pointers.

Jim
Was This Post Helpful? 4
  • +
  • -

Page 1 of 1