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;
Related
When I'm writing a header file, the editor can't find <iostream>. And the std namespace can't be detected as well.
num.h
#include "tag.h"
#include "token.h"
#include <iostream>
#include <stdio.h>
#include <string>
#ifndef NUM_H_
#define NUM_H_
class Num : public Token {
public:
int value;
Num(int v) : Token(Tag::NUM) { value = v; };
std::string toString() {return " "+std::to_string(value);}
};
#endif
But the strange thing is, when I create a file that includes this header file, and write some demo code to test if this header file really works, then it works fine.
num.cpp
#include "num.h"
int main() {
Num n(1);
std::string a = n.toString();
std::cout<<a;
}
Here is what I get after running it:
I also find that it's fine to write #include <iostream> in the .cpp file, there won't be any error.
It's quite strange, I don't know what's wrong. Maybe it's a bug in VScode?
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"
So I've done extensive googling and searching on StackOverflow and am unable to find a solution despite several answers with this exact issue.
I am trying to create a test class in an external file called Fpc5.cpp
It's contents are:
Fpc5.cpp
#include "stdafx.h"
#include "Fpc5.h";
#include <iostream>
using std::cout;
class Fpc5 {
int bar;
public:
void testMethod();
};
void Fpc5::testMethod() {
cout << "Hey it worked! ";
}
and my main .cpp file:
Test.cpp
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
//#include "Fpc5.cpp"
#include "Fpc5.h";
using std::cout;
using std::cin;
using std::endl;
int main()
{
cout << "Hello" << endl;
Fpc5 testObj;
testObj.testMethod();
system("pause");
return 0;
}
all the answers I've read indicate this is caused becaused I used to be including the class in the main file itself which is why I created a header file
Fpc5.h
#pragma once
void testMethod();
This changed the error, but still did not fix the issue. Currently my Test.cpp does not recognize a Fpc5 class. I've also tried adding the Fpc5.cpp and Fpc5.h in stdafx.h and that still does not resolve the issue.
stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
//#include "Fpc5.cpp"
#include "Fpc5.h"
I'm sure this a simple syntax/conceptual understanding error, but I'm quite new to c++ and am not sure what is wrong.
This is definition of your class and it must be in Fpc5.h
class Fpc5 {
int bar;
public:
void testMethod();
};
Then, you have Fpc5.cpp where you implement methods of the class:
#include "Fpc5.h" // Compiler needs class definition to compile this file!
void Fpc5::testMethod()
{
}
And then you can use Fpc5 class in Test.cpp
#include "Fpc5.h"
int main()
{
Fpc5 foo;
foo.testMethod();
return 0;
}
As an alternative you can pack everything into Test.cpp
Move the definition of your class:
class Fpc5 {
int bar;
public:
void testMethod();
};
to the header file, "Fpc5.h".
Implement the methods to "Fpc5.cpp".
Here is my main.cpp:
#include <iostream>
#include "function.cpp"
using namespace std;
extern int giveMain();
int main() {
int x = 4;
x = giveMain(x);
cout << x << endl;
}
And here is my function.cpp:
#include <iostream>
using namespace std;
int giveMain(int a) {
a = 3 + a;
return a;
}
But when I compile, it says that "Linker command failed". Can anyone helps me to solve this problem.
You declared the function int giveMain() in main.cpp but the function in function.cpp takes an int. Declare the correct function and it should work. Also extern is the default for functions so you don't need to include the keyword.
EDIT: Just noticed that you #include <function.cpp> in main.cpp. Never include .cpp files. The issue you were having was multiple definitions for int giveMain(int) because the contents of functions.cpp was being compiled twice.
I really don't understand how to fix this redefinition error.
COMPILE+ERRORS
g++ main.cpp list.cpp line.cpp
In file included from list.cpp:5:0:
line.h:2:8: error: redefinition of âstruct Lineâ
line.h:2:8: error: previous definition of âstruct Lineâ
main.cpp
#include <iostream>
using namespace std;
#include "list.h"
int main() {
int no;
// List list;
cout << "List Processor\n==============" << endl;
cout << "Enter number of items : ";
cin >> no;
// list.set(no);
// list.display();
}
list.h
#include "line.h"
#define MAX_LINES 10
using namespace std;
struct List{
private:
struct Line line[MAX_LINES];
public:
void set(int no);
void display() const;
};
line.h
#define MAX_CHARS 10
struct Line {
private:
int num;
char numOfItem[MAX_CHARS + 1]; // the one is null byte
public:
bool set(int n, const char* str);
void display() const;
};
list.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "list.h"
#include "line.h"
void List::set(int no) {}
void List::display() const {}
line.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "line.h"
bool Line::set(int n, const char* str) {}
void Line::display() const {}
You need to put include guards in your headers.
#ifndef LIST_H_
#define LIST_H_
// List.h code
#endif
In list.cpp, you are including both "line.h" and "list.h". But "list.h" already includes "line.h" so "list.h" is actually included twice in your code. (the preprocessor is not smart enough to not include something it already has).
There are two solutions:
Do not include "list.h" directly in your list.cpp file, but it is a practice that does not scale: you have to remember what every of your header file includes and that will be too much very quickly.
use include guards, as explained by #juanchopanza
You are including "line.h" twice, and you don't have include guards in your header files.
If you add something like:
#ifndef LINE_H
#define LINE_H
... rest of header file goes here ...
#endif
to your header files, it will all work out fine.