visual c++ - C++ Header #define compiler error -
i'm learning c++ , i'm @ low level still. - i'm looking @ using header files , have 2 part question.
part 1
as understand them far, header file definitions similar hard coded strings within vb.net? example can following #define stock_quantity 300
, reference definition when using within functions? assume works same way vb.net strings need change value in 1 place should need make changes definition , program references number 300 on few hundred different lines?
part 2
now, said i'm still learning i'm still doing ye old multiplication tasks. i'm within using functions main .cpp file i'm not moving on header files. code snippet far.
add.h
#ifndef add_h #define add_h int add(int x, int y); #endif
main.cpp
#include "stdafx.h" #include <iostream> #include "add.h" int main() { using namespace std; cout << add(3, 4) << endl; return 0; }
when trying run receive 2 build errors , not compile.
apologies these silly questions, appreciate insight, tips or other things should consider. thanks.
edit
based on answer i've changed add.h too
#ifndef add_h #define add_h int add(int x, int y) { return x + y; } #endif
as read answer realised hadn't told function :( - gosh.
you have not added function body function add
int add(int x, int y) { // add stuff here }
this can done in either header file, or seperate cpp file add. trying call function has no code. known function prototype.
Comments
Post a Comment