I have a query for creating c++ object inside c file.
I have the sample code below. When trying to import the CPlusHeader it throws an error which i could not understand.
The error is iostream' file not found as one of the error. How could i resolve this issue.
Regards,
Lenin
CPlusFile.h
include iostream
include string
using namespace std;
class CPlusFile {
int data;
public:
CPlusFile();
int getData();
};
CPlusFile.cpp
CPlusFIle::CPlusFIle() {
data = 10;
}
int CPlusFile::getData() {
return data;
}
CFile.h
int doSomething();
CFile.c
include "CFile.h"
include "CPlusFile.h"
int doSomething() {
CPlusFile object;
}
It strongly depends on what you call a "C file". Previous answer assumed that you meant a file with a .c suffix. I assume here that you mean a file that shall be compiled with a C compiler.
If my assumption is valid, then the answer is simple: You cannot instantiate C++ classes in a C file. What you can do, though, is call C++ static methods from the C code. Please refer, for example, to In C++ source, what is the effect of extern "C"? to see how to do this.
First of all, it is
#include <iostream>
#include <string>
and not
include iostream
include string
Second, if CFile.c is compiled as C, then this will not work. The C compiler will not understand the class keyword and you cannot create an instance of a class in C.
iostream is a C++ header, and isn't available if you're compiling using a C compiler. You can write C++ code in a .c file, you just need to use the right compiler.
You can use this in your C++ header file to check wether you are going to include it from C or C++ code:
#ifdef __cplusplus
The includes iostream and others, as well as using class, are only available for C++ code.
But if you want to use the CPlusFile class, which is a C++ class, you can only do that in C++ code. Best is to rename your CFile.c to CFile.cpp.
Yes, it is possible to call C++ object inside the C file. Here I performed a scenario and
it's working fine for me.
CPlusFile.h
#include<iostream>
#include<string>
using namespace std;
class cplus{
int data;
public:
cplus();
int getdata();
};
CPlusFile.cpp
#include "cplusfile.h"
cplus::cplus(){ data =10; }
int cplus::getdata(){ return data; }
CFile.h
#include "cplusfile.h"
#include<stdio.h>
int dosomething();
CFile.c
#include "cfile.h"
int dosomething(){
cplus c;
printf("%d",c.getdata());
}
int main() {
dosomething();
return 0;
}
And compile this by g++ CFile.c CPlusFile.cpp and it works fine.
Related
I'm comparatively new to C++ so I tested some things out in Xcode, and found a really weird thing.
This is my 'Testing.h' file
#ifndef Testing_h
#define Testing_h
class Testing{
private:
int a;
public:
Testing(int a=3);
void hey(int b);
};
#endif
This is my 'Testing.cpp' file
#include "Testing.h"
Testing::Testing(int a){
a = 4;
}
And finally, this is the 'main.cpp' file
#include <iostream>
#include "Testing.h"
using namespace std;
int main(){
Testing a;
//Apparently not completing the definitions of every abstract methods in the class is not a problem
}
I only declared 'void hey(int b)' in 'Testing.h' but have not defined it in 'Testing.cpp'. So I was wondering how it is possible for the compiler to successfully compile the 'main.cpp' without having enough information of 'void hey(int b)'. Thanks in advance!
Because you never require there to be a definition for hey().
You can require a definition by calling it, for example :
a.hey(42);
And you'll see that the linker isn't too happy because hey is an undefined reference.
Testing a;//Apparently not completing the definitions of every abstract methods in the class is not a problem
You defined constructor with default value a=3 but calling both constructor argument and class parameter the same name is bad practice.
Instead you can write this:
//Testing.h
#ifndef Testing_h
#define Testing_h
using namespace std;
class Testing{
private:
int number;
public:
Testing(int a=3): number(a = 4){}//it's the same as your implementation in cpp file
void hey(int b);
int getNumber() {return number;}
};
#endif
//main.cpp
#include <iostream>
#include "Testing.h"
int main()
{
Testing object;
cout<<object.getNumber();// returns 4
return 0;
}
And why hey compiles?
During building your project compiler translates your source code into object code by verifying the syntax. After that process linker checks the definitions marked by whole phrases. Source code is compiled from each file provided. Linker doesn't care for the implementation presence, it only looks it up if a method is used by the program. So even without implementation of hey your program compiles.
Last remark
It's discouraged to include .cpp files use headers instead. Sometimes you can get yourself into multiple definitions of the same functions causing compiler errors.
id.cpp
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
static int a;
};
int A::a=20;
class b
{
public:
b()
{
cout<<A::a<<endl;
}
};
int main()
{
b *b1 = new b();
return 0;
}
id1.cpp
#include "stdafx.h"
#include <iostream>
using namespace std;
class c
{
public:
int get()
{
cout<<A::a<<endl;
}
};
int main()
{
c c1;
c1.get();
return 0;
}
This is the way they have declared and got the output in one program but when I'm trying it I'm getting errors as the class is not a namespace or the program id is not included in the id1 file... How to get the variable that is stored in one file into the other file without using namespace and including the header file is there any option for it?
Two separate programs as shown (they're separate because they both define main()) cannot share variables in any simple way.
If the two separate files were to be integrated into a single program, so one of the main() programs was replaced, then you would fall back on the standard techniques of declaring the variable A::a in a header and using that header in both modules. The header would also define class A. This is the only sane way to do it.
You could write the definition of the class twice, once in each file, and declare the variable as an extern in one file and define it in the other, but that is not particularly sensible even in this simple case and rapidly degenerates into unmaintainable disaster if the code gets any more complex and there are more shared variables.
Of course, you might want to consider not using a global variable at all, but provide instead an accessor function. However, you still end up with a header declaring the services provided by the class A and the code implementing those services, and the code consuming those services.
The biggest problem I seem to run into when coding in c++ is the fact that you must declare a class before you can reference it. Say I have two header file like this...
Header1.h
#include "Header2.h"
#include <deque>
#include <string>
#include <iostream>
using namespace std;
class HelloPackage;
class Hello
{
public:
string Message;
HelloPackage * Package;
Hello():Message("")
{
}
Hello(string message, HelloPackage * pack)
{
Message = message;
Package = pack;
}
void Execute()
{
cout << Message << endl;
//HelloPackage->NothingReally doesn't exist.
//this is the issue essentially
Package->NothingReally(8);
}
};
Header2.h
#include "Header1.h"
#include <deque>
#include <string>
#include <iostream>
using namespace std;
class HelloPackage
{
public:
deque<Hello> Hellos;
HelloPackage()
{
Hellos = deque<Hello>();
}
int AddHello(string Message)
{
Hellos.push_back(Hello(Message,this));
}
void ExecuteAll()
{
for each(Hello h in Hellos)
h.Execute();
}
int NothingReally(int y)
{
int a = 0;
a += 1;
return a + y;
}
}
What I'm wondering is, is there any elegant solution for dealing with these issues? In say c#, and java, you're not restricted by this "linear" compiling.
Use header include guards, either "#ifndef / #define / #endif", or "#pragma once"
Put your code in a .cpp, not inline in the header
???
Profit
The reason this will work for you is because you can then use forward declarations of the class you want to reference without including the file if you so wish.
You are missing include guards
why define methods in the header?
Besides these problems with your code, to answer your question : normal way is to forward declare classes - not to include headers in headers (unless you have to).
If you follow a few basic rules, it is not awkward at all. But in comparison to e.g. Java or C#, you have to follow these rules by yourself, the compiler and/or language spec does not enforce it.
Other answers already noted that, but I will recap here so you have it in one place:
Use include guards. They make sure that your header (and thus your class definition) is only included once.
Normally, you will want to separate the declaration and implementation of your methods. This makes the header files more reusable and will reduce compilation time, because the header requires normally fewer #includes than the CPP (i.e. implementation) file.
In the header, use forward declarations instead of includes. This is possible only if you just use the name of the respective type, but don't need to know any "internals". The reason for this is that the forward declaration just tells the compiler that a certain name exists, but not what it contains.
This is a forward declaration of class Bar:
class Bar;
class Foo {
void foooh(Bar * b);
};
Here, the compiler will know that there is a Bar somewhere, but it does not know what members it has.
Use "using namespace xyz" only in CPP files, not in headers.
Allright, here comes your example code, modified to meet these rules. I only show the Hello class, the HelloPackage is to be separated into header and CPP file accordingly.
Hello.h (was Header1.h in your example)
#include <string>
class HelloPackage;
class Hello
{
public:
Hello();
Hello(std::string message, HelloPackage * pack);
void Execute();
private:
string Message;
HelloPackage * Package;
};
Hello.cpp
#include "Hello.h"
#include "HelloPackage.h"
using namespace std;
Hello::Hello() : Message("")
{}
Hello::Hello(string message, HelloPackage * pack)
{
Message = message;
Package = pack;
}
void Hello::Execute()
{
cout << Message << endl;
// Now accessing NothingReally works!
Package->NothingReally(8);
}
One question that may arise is why is the include for string is needed. Couldn't you just forward declare the string class, too?
The difference is that you use the string as embedded member, you don't use a pointer to string. This is ok, but it forces you to use #include, because the compiler must know how much space a string instance needs inside your Hello class.
I"m having some problem with the following program. The program implements a stack using a linked list. I'm not showing all my code here because the code is fine. But the problem I'm having is with linking different files together.
I'm using an IDE to run the program. When I run the TestIntStacks.cpp, the main method is supposed to call test() from StackFunctions.cpp. The test function (defined in StackFunctions.cpp), uses the TestStack class methods.
Currently I'm receiving an error saying "linker error, push/pop not defined". What I'm doing wrong? I'm sure it's something to do with a namespace.
MyStack.h
-------------------------------------
namespace A
{
class Node{
public :
char data;
StackNode* link;
StackNode(int v=0): data(v), link(NULL){ }
};
class MyStack{
private:
Node * top;
public:
MyStack():top(NULL){ }
void push(int c);
};
}//namespace
//TestStack.cpp
--------------------------------------------------------------
#include "MyStack.h"
namespace A
{
void MyStack::push(int x)
{
StackNode *temp = new StackNode(x);
temp->link = top;
top = temp;
}
}
//StackFunctions.cpp
-----------------------------------------------------------
#include <iostream>
#include "TestStack.h"
using namespace std;
using namespace A;
void test()
{
MyStack st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
}
// TestIntStacks.cpp
----------------------------------------------------------------
// Code for testing the TestStack
// from the A namespace.
#include <iostream>
#include <vector>
using namespace std;
#include "TestStack"
#include "StackFunctions.cpp"
void test();
int main()
{
test();
system("pause");
return 0;
}
You are defining push() and pop() methods in your header file TestStack.h, but you've not provided implementations for them in TestStack.cpp. You need to add the code that does the push and pop operations on your object.
This error seems pretty clear to me. You declared push() and pop() in your header file, but the linker could not find where these methods are implemented.
Where are they defined?
I think it has to do with the arguments provided to linker. For example, a similar error occurs when you use Visual C++ 6 in a following way. Let's say you created .cpp and .h files for a class. If you do not include cpp file into your project you get the similar error. Because the IDE does not determine the source file based on the provided header file. I don't know about dev-c++ IDE, but the solution might be similar. The problem is you compile (or not) TestStack.cpp and the output of this compiling is not provided to the linker, so the linker can't find the implementation.
You need to force the build script to use both cpp files. If you wrote your own make file, you need to build intermediate objects for each source, and then link at the end.
I suspect DEV-C++ doesnt automatically generate object files or try to link everything together.
Following an example at: http://www.learncpp.com/cpp-tutorial/47-structs/ relating to structs, and when I tried to compile this program:
#include <iostream>
void PrintInformation(Employee sEmployee)
{
std::cout<<"ID: "<<sEmployee.nID<<std::endl;
std::cout<<"Age: "<<sEmployee.nAge<<std::endl;
std::cout<<"Wage: "<<sEmployee.fWage<<std::endl;
}
struct Employee {int nID;int nAge;float fWage;};
int main()
{
Employee abc;
abc.nID=123;
abc.nAge=27;
abc.fWage=400;
// print abc's information
PrintInformation(abc);
return 0;
}
I get the following:
Why is that?
Thanks.
You need to declare the struct before the function that attempts to use it.
C (and by extension, C++) were designed for "single-pass" compilation. Therefore, everything must be available to the compiler by the time it's required.