Error when separating function into another source file in C - c++

I have a simple project:
method.h:
#pragma once
#ifdef _METHOD_
#define _METHOD_
#include <stdio.h>
#include <conio.h>
int plus(int a, int b);
#endif // _METHOD_
method.cpp:
#include "method.h"
int plus(int a, int b)
{
return a+b;
}
Source.cpp:
#include <stdio.h>
#include <conio.h>
#include "method.h"
void main()
{
int a = plus(4, 5);
printf("%d",a);
printf("\n");
_getch();
}
but when I build project, an error occure:
I'm a newbie in C programming.
And so sorry about my grammar mistakes

remove
#ifdef METHOD
#define METHOD
as #pragma once does the same and if you want to use guards it should be
#ifndef ....
#ifdef _METHOD_ will ignore the header file as you are never defining "_METHOD_"
Update #1
As per MSDN on #pragma once;
Specifies that the file will be included (opened) only once by the
compiler when compiling a source code file.

Firstly, change "#ifdef METHOD" in your header file to "#ifndef METHOD"

Related

Undefined Reference when compiling C++

My code is similar to this one, but the problem is exactly the same: I'm getting an "undefined reference to `Test1::v" in Test1.cpp and in Test2.cpp when compilling the program in VSCode. What am I doing wrong? I'm a bit new on c++ so I just downloaded an extension that made me a project in c++ automatically. When I run the program using Ctrl + Shift + B it gives me this error, but when I do it with the Code Runner extension it doesn't detect the .cpp files.
// Test1.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST1_H
#define TEST1_H
class Test1{
public:
Test1();
static vector<Test1> v;
int a;
};
#endif
//Test1.cpp
#include "Test1.h"
Test1::Test1(){
a = 2;
v.push_back(*this);
}
//Test2.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST2_H
#define TEST2_H
class Test2{
public:
Test2();
double var;
};
#endif
//Test2.cpp
#include "Test2.h"
#include "Test1.h"
Test2::Test2(){
var = 5;
Test1::v[0].a += var;
}
//main.cpp
#include <iostream>
#include "Test1.h"
#include "Test2.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
}
You have declared the static vector in the header file, but you need to define it in a cpp file. Add:
vector<Test1> Test1::v;
to your test1.cpp file. You can learn more about definition vs declaration here.
Also make sure you read this: Why is "using namespace std;" considered bad practice?
You could prepend the class name to call the variable directly since it's static. So, you could do something like:
Test1::Test1(){
// v.push__back(*this); // previous
Test1::v.push_back(*this); // now
}
in Test1.cpp. You'll then get a reference tooltip on your VS Code:
static std::vector<Test1> Test1::v
Which proves it's done.

can i compile .cpp file in to a dll file where the cpp file itself calls the functions from another dll file

So getting in to detail of my question :
I have cpp file which calls functions from a dll file which is written in c.
Here is just an example code
first.cpp:
#include <stdio.h>
#include "Header.h"
#include <windows.h>
typedef double(*MYFUN1)(double);
int main1(int k )
{
MYFUN1 pfun1;
HMODULE hMod;
hMod = LoadLibrary(L"MyMathDll.dll");
pfun1 = (MYFUN1)GetProcAddress(hMod, "PowerOf3");
int ii, max = 20;
for (ii = 0; ii < max; ii++)
{
printf("%le", pfun1(10));
}
return ii;
}
header.h
#pragma once
int main1(int k);
My MyMathDll.dll is formed by following two files:
MyMathDll.c
#include "MyMathDll.h"
double PowerOf3(double UserNumber)
{
return UserNumber * UserNumber * UserNumber;
}
MyMathDll.h
#include <stdio.h>
#if defined (WIN32)
#if defined(FUNCTIONS_STATIC)
#define FUNCTIONS_API
#else
#if defined(FUNCTIONS_EXPORTS)
#define FUNCTIONS_API __declspec(dllexport)
#else
#define FUNCTIONS_API __declspec(dllimport)
#endif
#endif
#else
#define FUNCTIONS_API
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef MYMATHDLL_EXPORTS
#define MYMATHDLL_API __declspec(dllexport)
#else
#define MYMATHDLL_API __declspec(dllimport)
#endif
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
MYMATHDLL_API double PowerOf3(double UserNumber);
#ifdef __cplusplus
}
#endif
Now I am compiling first.cpp to a dll file, so it's basically i am trying make a dll file which already calls the functions from another c dll file.
I built the project a dll file is generated. Now I am making new project with application type .exe and trying load the dll file which is generated from first.cpp
example code of the second cpp which calls the functions from first.cpp
second.cpp
#include <stdio.h>
#include <windows.h>
typedef int (*MYFUN4)(int );
void main()
{
int k = 10;
MYFUN4 pfun4;
HMODULE hMod2;
hMod2 = LoadLibrary(L"first.dll");
pfun4 = (MYFUN4)GetProcAddress(hMod2, "main1");
printf("%d", pfun4(k));
getchar();
}
when i try to run this application, I get a pop window with following message:
Exception triggered at 0x00000000 in ConsoleApplication7.exe: 0xC0000005: Access violation when running at position 0x00000000.
If there is a handler for this exception, the program may still run safely.

Compiler acts as if a preprocessor directive isn't defined

I have a header file and its cpp file (Error.h, Error.cpp). The cpp file performs a check on a preprocessor directive but it always fails.
Error.h:
/*
Optional macros:
AE_EXIT_AT_ERROR
AE_CONSOLE_WRITE_AT_ERROR
*/
#pragma once
extern void aeError(const char *str, int code=1);
extern void aeAssert(bool b, const char *failStr = "assertion failed");
Error.cpp:
#include "Error.h"
#include <stdexcept>
#ifdef AE_CONSOLE_WRITE_AT_ERROR
#include <iostream>
#endif
void aeError(const char *str, int code)
{
#ifdef AE_CONSOLE_WRITE_AT_ERROR
std::cout << str << std::endl;
#endif
throw std::runtime_error(str);
#ifdef AE_EXIT_AT_ERROR
std::exit(code);
#endif
}
void aeAssert(bool b, const char *failStr)
{
if(!b)
aeError(failStr);
}
main.cpp:
//define both macros:
#define AE_CONSOLE_WRITE_AT_ERROR
#define AE_EXIT_AT_ERROR
#include "Error.h"
//rest of code
//...
both std::cout << str << std::endl; and std::exit(code); don't get compiled (I checked it "manually", although they are also marked gray by the IDE, which is VS2010).
What might be the cause of this?
main.cpp and Error.cpp are different translation units. You define the macro only for main.cpp, not for Error.cpp.
You should either put your #define directives in a header file included by both .cpp files, or define these macros in project settings/makefile.

Compiling Helper file with functions

I'm at a loss - i'm just getting into C++ and for some reason this is not working out for me. So i'm using Netbeans, and i've got the following main file:
#include <cstdlib>
#include "functions.h"
using namespace std;
int main(int argc, char** argv) {
f("help");
return 0;
}
Functions.h file:
#include <string>
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void f( string a );
#endif
and Functions.cpp file:
#include "functions.h"
void f( string a ) {
return;
}
So, long story short, it doesn't compile. It says it can't understand the string variable? I don't get it, i tried moving the include for string all over the place but nowhere seems to help. What do i do?
If you are attempting to use std::string, you have to #include <string> in your functions header, and call it std::string,since it is in the std namespace.
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
void f( std::string a );
#endif
See this related post and also why is 'using namespace std' considered bad practice in C++?
You need to include string header file in Functions.h, also tell compiler that string is from std namespace.
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
void f( std::string a );
#endif
Functions.cpp file:
#include "functions.h"
void f( std::string a ) {
return;
}
Better practice is to pass string by const reference
void f(const std::string& a ) {
return;
}
See Why is 'using namespace std;' considered a bad practice in C++?
Include the standard header: <string>
#include <string>

how to use complex library in a header file?

I have written a piece of code that uses complex library. I have put the definition of my function in a header file and in the main.cpp i have defined the complex number "I" as you can see in my codes below.
But when i want to compile this code i receive errors.
I think the function in the header file can not use complex library.
How can i fix this problem?
Thanks.
main.cpp
#include <iostream>
#include "math.h"
#include <complex>
#include "header.h"
using namespace std;
typedef complex<double> cmp;
cmp I(0.0,1.0);
int main()
{
cout << function(5.0) << endl;
return 0;
}
header.h
#ifndef header
#define header
double function(double x)
{
return 5*exp(I*x).real();
}
#endif
The problem is that I is not defined when your header file is parsed. You would need to move the definition of I before the #include "header.h".
Because I is defined after the header include, your main.cpp essentially becomes this:
double function(double x)
{
return 5*exp(I*x).real();
}
using namespace std;
typedef complex<double> cmp;
cmp I(0.0,1.0);
The compiler parses your function, and throws an error because it doesn't know what I is (yet).
You should include any constants that functions rely upon before the function, like this in your header file:
#ifndef header
#define header
#include <complex>
typedef complex<double> cmp;
cmp I(0.0,1.0);
double function(double x)
{
return 5*exp(I*x).real();
}
#endif
Change your header.h to contain this:
double function( std::complex<double> I, double x)
Change your main.cpp so it contains this:
cout << function(I, 5.0) << endl ;
You problem was because in your header.h you used variable I which was not visible.
You should include the library header file in your header(header.h) which uses the library symbols.
#include <complex>
It is always better to include all the dependencies of an file within itself rather than depending on them to be included otherwise. Maybe your header.h gets included somewhere the library symbol name dependency does not get resolved indirectly through other includes.
EDIT:
On a side note I am not sure why you included the definition of the function in the header file itself. You should, change the header to only have the declaration:
header.h
#ifndef header
#define header
#include <complex>
typedef std::complex<double> cmp;
extern cmp I;
double function(double x);
#endif
Add another source file which defines the function
header.cpp
#include "header.h"
double function(double x)
{
return 5*exp(I*x).real();
}
main.cpp
#include <iostream>
#include "math.h"
#include <complex>
#include "header.h"
using namespace std;
cmp I(0.0,1.0);
int main()
{
cout << function(5.0) << endl;
return 0;