I am a CS student and I am having trouble getting Dev C++ to compile multiple files in a project when working with classes. The problem is that a function prototyped in a class definition won't compile if the function is coded outside of the class. The code attached compiles and runs fine without a project, but does not compile if I try to move it into a project. I get linker errors about multiple definitions. I don't understand why the OOP version is different than a prototype and later function code in a structured program.
Also, I had the same exact problem with code from the Malik textbook (C++ Programming). It also compiled and ran without a project, but failed within a project. I think that the problem is the "project" in some way, not the code, but I am not sure.
I created the project using "File-New-Project-Console Application".
I seem to have two choices: don't use projects, or keep all functions fully inside of the class definitions. Or a third - learn something new?
Here is the code (3 files):
//*******
// FILE NAME: main.cpp
// simple beginnings of a binary tree program
// doesn't do anything yet, but should compile?
//*******
include <iostream>
include <iomanip>
include <cstddef>
include "Tree.cpp"
using namespace std;
int main(int argc, char *argv[])
{
Tree tree;
cout << "*** Binary Trees ***\n";
system("PAUSE");
return 0;
} // end main()
//*****
// FILE NAME: Tree.cpp
// DESCRIPTION: class
//*****
ifndef TREE
define TREE
include <cstddef>
include "Node.cpp"
using namespace std;
class Tree {
protected:Node*root;public:// constructor, creates an empty tree// Tree() { root = NULL; } // do it this way and it compiles// if we comment out Tree(); and the function belowTree(void);// this version crashes - why?
};
// why doesn't this work in a project?
Tree::Tree(void) {
root = NULL;
}
endif
//******
// FILE NAME: Node.cpp
// DESCRIPTION: class
//******
ifndef NODE
define NODE
include <cstddef>
class Node {
protected:Node*left,*right;public:// constructor, creates an empty treeNode(){left=right=NULL;}
};
//Node::Node() {
// left = right = NULL;
//}
endif
Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks. It took me a few tries to figure out what you meant, but it worked. I needed to eliminate all #include of cpp files in other cpp files, and keep only the header in both cpp files.
Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-11-06
Suppose this header:
// header1.h ----------
class myClass {
void method() { /.../} // definition on-line
};
// end header --------
That work fine. Now you declare the method off-line but still in the header:
Here you get an error with "this" compiler (is not enough smart) because each time you include the header there are a new definition of the method. The trickery is to move the definition to a regular .cpp file:
// header3.h -----------
class myClass {
void method(); // declaration
};
// end header3 ----------
// some cpp file --------
void myClass::method() { /.../} // definition off-line
/ some other code here /
// end regular file --------
I'v programmed a lot with BC++, and in such compiler that things does not happen, so your's problem happened to me the first times with this GNU compiler.
HTH
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-11-06
Of course, you need include the header3.h in the cpp file.
// some cpp file -------
include header3.h
void myClass::method() { /.../} // definition off-line
/ some other code here /
...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for the replies, but I tried what I think you sugested, and it still does not work for me. The code compiles outside of a formal Dev project, but not inside of a Dev project. Here is new code and the error message:
//*******
// FILE NAME: main.cpp
// simple thing
//*******
// compiles and runs outside of a Dev C++ "project" using Dev C++;
// won't compile inside of a Dev C++ project
// error:
// multile definition of 'Thing::Thing()'
// first defined here
// C:\Dev-Cpp\Examples\Thing\Makefile.win [Build Error][Thing.exe] Error 1
I thought that the whole point of the #ifndef stuff was to prevent multiple inclusions, which don't exist in a program this simple anyway? Sorry, I am still confused.
vettejeep
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You need to include Thing.h not Thing.cpp in main. The cpp file will get compiled and linked separately. All you need to include in main is the header file.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am a CS student and I am having trouble getting Dev C++ to compile multiple files in a project when working with classes. The problem is that a function prototyped in a class definition won't compile if the function is coded outside of the class. The code attached compiles and runs fine without a project, but does not compile if I try to move it into a project. I get linker errors about multiple definitions. I don't understand why the OOP version is different than a prototype and later function code in a structured program.
Also, I had the same exact problem with code from the Malik textbook (C++ Programming). It also compiled and ran without a project, but failed within a project. I think that the problem is the "project" in some way, not the code, but I am not sure.
I created the project using "File-New-Project-Console Application".
I seem to have two choices: don't use projects, or keep all functions fully inside of the class definitions. Or a third - learn something new?
Here is the code (3 files):
//*******
// FILE NAME: main.cpp
// simple beginnings of a binary tree program
// doesn't do anything yet, but should compile?
//*******
include <iostream>
include <iomanip>
include <cstddef>
include "Tree.cpp"
using namespace std;
int main(int argc, char *argv[])
{
} // end main()
//*****
// FILE NAME: Tree.cpp
// DESCRIPTION: class
//*****
ifndef TREE
define TREE
include <cstddef>
include "Node.cpp"
using namespace std;
class Tree {
};
// why doesn't this work in a project?
Tree::Tree(void) {
root = NULL;
}
endif
//******
// FILE NAME: Node.cpp
// DESCRIPTION: class
//******
ifndef NODE
define NODE
include <cstddef>
class Node {
};
//Node::Node() {
// left = right = NULL;
//}
endif
Thanks.
Thanks. It took me a few tries to figure out what you meant, but it worked. I needed to eliminate all #include of cpp files in other cpp files, and keep only the header in both cpp files.
Thanks.
Suppose this header:
// header1.h ----------
class myClass {
void method() { /.../} // definition on-line
};
// end header --------
That work fine. Now you declare the method off-line but still in the header:
// header2.h ------------
class myClass {
void method(); // declaration
};
void myClass::method() { /.../} // definition off-line
// end header2 -----------
Here you get an error with "this" compiler (is not enough smart) because each time you include the header there are a new definition of the method. The trickery is to move the definition to a regular .cpp file:
// header3.h -----------
class myClass {
void method(); // declaration
};
// end header3 ----------
// some cpp file --------
void myClass::method() { /.../} // definition off-line
/ some other code here /
// end regular file --------
I'v programmed a lot with BC++, and in such compiler that things does not happen, so your's problem happened to me the first times with this GNU compiler.
HTH
Of course, you need include the header3.h in the cpp file.
// some cpp file -------
include header3.h
void myClass::method() { /.../} // definition off-line
/ some other code here /
...
Thanks for the replies, but I tried what I think you sugested, and it still does not work for me. The code compiles outside of a formal Dev project, but not inside of a Dev project. Here is new code and the error message:
//*******
// FILE NAME: main.cpp
// simple thing
//*******
// compiles and runs outside of a Dev C++ "project" using Dev C++;
// won't compile inside of a Dev C++ project
// error:
// multile definition of 'Thing::Thing()'
// first defined here
// C:\Dev-Cpp\Examples\Thing\Makefile.win [Build Error] [Thing.exe] Error 1
include <iostream>
include <iomanip>
include "Thing.cpp"
using namespace std;
int main(int argc, char *argv[])
{
} // end main()
//*****
// FILE NAME: Thing.cpp
// DESCRIPTION: class
//*****
ifndef THINGCPP
define THINGCPP
include "Thing.h"
Thing::Thing() {
data = 13;
}
endif
//*****
// FILE NAME: Thing.h
// DESCRIPTION: header
//*****
ifndef THINGH
define THINGH
class Thing {
};
endif
I thought that the whole point of the #ifndef stuff was to prevent multiple inclusions, which don't exist in a program this simple anyway? Sorry, I am still confused.
vettejeep
You need to include Thing.h not Thing.cpp in main. The cpp file will get compiled and linked separately. All you need to include in main is the header file.