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
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 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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.