d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Paying 250 Fg To Help With A Problem
Add Reply New Topic New Poll
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
May 15 2013 04:09am
This is the final topic I'm making. This isn't the full program, I only included relevant parts. It's probably very simple for you guys.

I have 3 classes, 1 creates objects that hold a name (string), "DatabaseNames", 1 creates objects that hold an address (string), "DatabaseAddress", and the last 1 is "DatabaseGeneral" which creates 100 objects from each class. I want to implement a function below... DatabaseAddress getAddress(DatabaseNames id); inside the DatabaseGeneral class. (see below)

(DatabaseNames.h)
Code

#ifndef DATABASENAMES_H
#define DATABASENAMES_H
#include <string>

class DatabaseNames {
public:
   std::string name;
};


(DatabaseAddress.h)
Code

#ifndef DATABASEADDRESS_H
#define DATABASEADDRESSL_H
#include <string>

class DatabaseAddress {
public:
   std::string address;
};


(DatabaseGeneral.h)
Code

#ifndef DATABASEGENERAL_H
#define DATABASEGENERAL_H
#include "DatabaseNames.h"
#include "DatabaseAddress.h"
#include <string>

class DnaDatabase {
public:
   DatabaseNames recordN[100];                     //holds 100 names, recordN[x] corresponds to recordA[x]
   DatabaseAddress recordA[100];                   //holds 100 addresses, of the 100 names
   
   void addRecord(DatabaseNames name, DatabaseAddress address);     //used to fill up the 200 objects
   DatabaseAddress getAddress(DatabaseNames id);                  //the member function thats giving me problems
};


I want to implement DatabaseAddress like so:

Code
DatabaseAddress getAddress(DatabaseNames id){
   for (int i = 0; i < 100;  i++){
       if (id.name == recordN[i].name){
           return recordA[i].address;
       }
   }
}


I know this doesn't work, I get a "recordN and recordA was not declared in this scope". But what's the easiest way to implement this without changing the member function's interface? i.e: DatabaseAddress getAddress(DatabaseNamesid);
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
May 15 2013 06:05am
I'm no c++ genius, but the error says you haven't declared the variables...
i.e. You need to declare them in your .cpp file not the .h file probably. (Then again, I don't really know c++...) You might also have to initialize them. In fact, I'd recommend initializing them. It's best practice imo.
You should declare the arrays as global variables, then in your constructor, initialize them.

This post was edited by DirtyRasa on May 15 2013 06:06am
Member
Posts: 1,550
Joined: Dec 5 2008
Gold: 1,465.77
May 15 2013 08:00am
Fixed Code.

Code
DatabaseAddress DnaDatabase::getAddress(DatabaseNames id){
  for (int i = 0; i < 100;  i++){
      if (id.name == recordN[i].name){
          return recordA[i];
      }
  }
}



Two errors.
1. You need to specify to the compiler that you are implementing a method of the DnaDatabase class and not a global function. (that's why recordN and recordA were undefined)
2. recordA[i] is an array of DatabaseAdress 's objects and address is an element of each of these objects with the type of string. In your method prototype you specify that you will return a DatabaseAddress object, but the method returns a the string element. (one of these must be changed)

This post was edited by EzRA on May 15 2013 08:01am
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
May 15 2013 03:01pm
Quote (EzRA @ May 15 2013 09:00am)
Fixed Code.

Code
DatabaseAddress DnaDatabase::getAddress(DatabaseNames id){
  for (int i = 0; i < 100;  i++){
      if (id.name == recordN[i].name){
          return recordA[i];
      }
  }
}



Two errors.
1. You need to specify to the compiler that you are implementing a method of the DnaDatabase class and not a global function. (that's why recordN and recordA were undefined)
2. recordA[i] is an array of DatabaseAdress 's objects and address is an element of each of these objects with the type of string. In your method prototype you specify that you will return a DatabaseAddress object, but the method returns a the string element. (one of these must be changed)


Sigh... I feel like an idiot, it was so simple. Nevertheless here's the fg
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll