Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How can I link header, implementation, and cpp?
New Posts  All Forums:Forum Nav:

How can I link header, implementation, and cpp?

post #1 of 5
Thread Starter 
Hello, I made a simple test. However, It gave me errors such as, multiple definition of Authenticate::username, first define here, multiple definition of Authenticate:assword, first define here, ld return 1 exit status, and Build error 1.

Header file: user1.h
Code:
#ifndef USER1_H
#define USER1_H
#include <iostream>
using namespace std;

namespace Authenticate{
      //check string and return true if it's all char.
      bool checkNumber(); 
      //check if input is 8 char + checkNumber.
      bool isValid();
   
      //request user name
      void inputUserName();  
      //request user password
      void inputPassword();
      //return username;
      string getUserName();
      //return password
      string getPassword();
 
      string username;
      string password;

}

#endif
Implementation: user1.cpp
Code:
#include <iostream>
#include "user1.h"
using namespace std;


namespace Authenticate{
  //if username contains number, return false.        
  bool checkNumber(){
    for(int i=0; i<8;i++){
      if(isdigit(username[i])){
        return false;
      }
    }
    return true;
  }   
    //if username !=8 or contains number, return false.     
    bool isValid(){
      if(username.length()==8&&checkNumber()){
        return true;
      }else{
        return false;
      }
    }
  //request name and check condition
  void inputUserName(){
    do{
      cout << "Enter your username(8 letters only)" << endl;
      cin >> username;
    }while (!isValid());
  }
  //return username
  string getUserName(){
    return username;
  }
  //request password and check condition.
  void inputPassword(){
    do{
      cout << "Enter your password(at least 8 characters " <<
              "and at least one non-letter)" << endl;
      cin >> password;
    } while(!isValid());
  }
  //return password
  string getPassword(){
    return password;
  }
  

}
and userInterface1.cpp
Code:
#include <iostream>
#include "user1.h"

int main(){
  using namespace Authenticate;
  //prompt and request username and password
  inputUserName();
  inputPassword();
  //display username and password.
  cout << "Your user name is " << getUserName() <<
          "and your password is: " <<
        getPassword() << endl;    
    
    
  cin.ignore(256, '\
');
  cout << "\
\
Press ENTER to continue..." << endl;
  cin.get();      
  return 0;    
}
I can't find any error and the error messages didn't provide any line. please help! thank you.
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
post #2 of 5
When you declare a variables in a header file, like you have done in user.h, every source file that includes that header, either directly or indirectly, gets its own separate copy of the variable.

Move the variables to user1.cpp and it should start to work.
Edited by nikolauska - 3/12/11 at 3:01am
post #3 of 5
EDIT: I think you need to include your user1.cpp file in your userinterface1.cpp (i.e. #include "user1.cpp").
Edited by TheSchlaf - 3/12/11 at 3:01am
post #4 of 5
Also, since you declared the namespace Authenticate in your main function, you'll need to prepend the namespace std and scope resolution operator before every input/output belonging to the std namespace.

Instead of

Code:
cout << ...
cin >> ...
You need

Code:
std::cout << ...
std::cin >>
post #5 of 5
Thread Starter 
Thank you. i found my own error. I have to use static on string username in order to access it from the header to the implementation file. Otherwise, it gave me an error that the object is missing. However, I was reading somewhere. It said it's not a good way to call non-static variable. Why?
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How can I link header, implementation, and cpp?