What is missing?what to do to solve the error?

0 Alam Khattak · December 30, 2014
/images/forum/upload/2014-12-30/b501642539c6b769f7674cf8eb24025a.jpg

Post a Reply

Replies

Oldest  Newest  Rating
0 Franz Schmidt · December 30, 2014
Ok first of all I hope you didn't write the add.h inside your main file?!?
Than where did you define your add function (you only show the protoype)


In your header file:
#ifndef ADD_H
#define ADD_H

int add(int x, int y){
return x + y;
}

#endif


In your main file:
#include <iostream>
#include "add.h"
using namespace std;

int main(){
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
return 0;
}
0 Alam Khattak · December 30, 2014
Franz let me add a snapshot of the same program.i guess i am still
 doing some silly mistake.pls don't mind in advance :)
/images/forum/upload/2014-12-30/f841b06e5eaa60cf7b4cffe65af85088.jpg
+1 Milan Obrenovic · December 30, 2014
No man, this is  how you do it:

Once you have your main.cpp file, go to File -> New -> Class...

As the Bucky said, uncheck "Has destructor" and name your class "add" or however you want. Also make sure it has been created in the same folder as your project.

This will give you two new files: add.h, add.cpp

After that, write the following code:


add.h
// If it hasn't been defined...
#ifndef ADD_H
// ...define it!
#define ADD_H

// This will create the function prototype
int add(int x, int y);

#endif



add.cpp
#include <iostream>
#include "add.h"

// This is actually declaring the function based on the function prototype
int add(int x, int y) {
    return x + y;
}



main.cpp
#include <iostream>
// Includes your header file where the function prototype was declared
#include "add.h"
using namespace std;

int main()
{
   // All you have to do is call the function and pass it two arguments
    cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
    return 0;
}
0 ahmedtarek mahmoud · December 30, 2014
i tired it there is a problem with the add .h i think it have to include it in his project folder itself not in the code like dirent .h as an example  
0 Alam Khattak · December 30, 2014
wow.that's it .thank u so much typex:)
0 Alam Khattak · December 30, 2014
http://www.learncpp.com/cpp-tutorial/19-header-files/

(THIS HAS CONFUSED Me.LATTER ON I SWITCHED TO BUCKY TUTOS)
On this page it is written as :


add.h:
#ifndef ADD_H
#define ADD_H
 
int add(int x, int y); // function prototype for add.h
 
#endif


#include <iostream>
#include "add.h" // this brings in the declaration for add()
 main.cpp that includes add.h:
int main()
{
    using namespace std;
    cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
    return 0;
}

    
  • 1

C++

107,251 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator