-3

main.h: header

#ifndef _MAIN_H
#define _MAIN_H
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct ComplexNum  
{
    double real;         
    double img;      
}mc;


char *JustShow(mc );

#endif // _MAIN_H

cal.cpp : function defined in another file called

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "main.h"

char *JustShow(mc a)
{
    char str1[100];
    char str2[100];
    char str3 [100];
    sprintf(str1,"%f",a.real);
    sprintf(str2,"%f",a.img);
    strcat(str1," + ");
    strcat(str1,str2);
    strcat(str1,"i");
    return(str1);

}

I tried to use the JustShow function in another file, but the compiler said undefined reference to 'JustShow'

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "main.h"

int main()
{
mc y;
y.img = 4;
y.real = 3;
printf("%s",JustShow(y));
}

I am so confused why can't I use the function JustShow when I had already added "main.h". I am also confused why should we define the functions in the source file instead of the header.
I'm sorry if this is the basic knowledge I should learn about and not ask others.

Samm
  • 1
  • 2
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – UnholySheep Nov 16 '20 at 16:12
  • 1
    how are you compiling, are you using `gcc main.c cal.cpp -o cal` ? – csavvy Nov 16 '20 at 16:12
  • by the way why it is `.cpp` file, you tagged `C` – csavvy Nov 16 '20 at 16:14
  • Seems fine and prints `3.000000 + 4.000000i`. Note there's a bug in cal.cpp - you cannot `return str1` because `str1` is on the stack. You might consider `return (strdup(str1))` instead. Also, you could just `sprintf(str1, "%f + %fi", a.real, a.img)` instead of all the strcat machinations. – jarmod Nov 16 '20 at 16:18
  • Problem solved, thanks! – Samm Dec 05 '20 at 14:06

1 Answers1

0

I'm going to call the third piece of code "main.cpp".

why can't I use the function JustShow when I had already added "main.h".

Header files only inform a compiler that a function, variable or constant exist. You need to compile cal.cpp with main.cpp: g++ main.cpp cal.cpp -o main

I am also confused why should we define the functions in the source file instead of the header.

  1. It makes code more organised.
  2. It speeds up compile time.

You can read more about header files here

Some advice for your code:

  1. Function JustShow is incorrect. Arrays are just pointers and you are returning a pointer to a local variable that are deleted after function returns. It means that it will be overwritten and you will lose data there (and trying to write there may crash your program). In order to make it work you can:
    • Give an array as an argument to the function. Then we would have void JustShow(mc a, char* str1);
    • create new array with new (or malloc if you wirte in C, not C++).
  2. You don't need to concat so much. sprintf can do all of that:
void JustShow(mc a, char* str1) {    
    sprintf(str1,"%f + %fi", a.real, a.img);
}
Nierusek
  • 333
  • 4
  • 13