0

for my c++ assignment, I just have to create a 'char', print it, then pass it as a reference argument to a function, modify it, and reprint it to prove that it has been changed. It seems easy, and I'm probably making a really dumb mistake, but i keep getting an error that says 'unresolved externals'. I've made a .cpp file and also declared a class in my header file.

My .cpp file:

#include <iostream>
#include <fstream>
#include "referenceshw.h"

using namespace std;

int main(){

    char s = 's';
    char& s1 = s;
    Ref test;

    std::cout << s <<endl;
    test.modify(s);

}

void modify(char& s1){

    s1 = 'd';
    std::cout << s1 <<endl;
    std::cout << s <<endl;

}

My header file:

#ifndef _REFERENCESHW_H
#define _REFERENCESHW_H

class Ref{

    public:
        char s;

void modify (char);

};

#endif
Oleksiy
  • 37,477
  • 22
  • 74
  • 122
kevlar924
  • 55
  • 1
  • 1
  • 6
  • 1
    You're using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Jul 23 '13 at 22:42
  • You also need to reference the class in the .cpp file; `void Ref::modify(char& s1) {}` – Steztric Jul 23 '13 at 22:54

2 Answers2

2

You function signatures dont match, in your .h you have:

void modify (char);

and in .cpp

void modify(char& s1){

simply add & after char, in the .h

Also because the function is defined outside the class declaration you need to add Ref:: in front of modify in your .cpp. In the end in your .cpp it should look like:

void Ref::modify(char& s1){

and in your .h

void modify(char&);
Borgleader
  • 15,826
  • 5
  • 46
  • 62
0

Borgleader is right. Other bugs: change

void modify(char& s1)

to

void Ref::modify(char& s1);

Also, are you trying to refer to a class member s1 in your code in modify()?

s1 = 'd'; // this will change the parameter that was passed in, 
          // is that what you want?
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Oleksiy
  • 37,477
  • 22
  • 74
  • 122