Unable to state function definition in c++ header file.

Write your question here.
I am conversant with specifying function declaration (of functions defined in other code files) in header file; as in C. But, seems cannot do this in C++. I have a code file GraphTest.cpp, and a header file graph.hpp. The main () is in the cpp file, & want to move main() in a seperate main.cpp file. But, am unable to rename main() in GraphTest.cpp, and get it called from the new main.cpp file. I placed (renamed) function declaration in graph.hpp file, but it didn't work.


The error in compilation is in newly created main.cpp file:

[Error] 'new_function' was not declared in this scope



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  Put the code you need help with here.
There are actually first two files, that want to make 3 files by having a seperate file to contain the main().


1. GraphTest.cpp //calls functions defined in graph.hpp
# include "graph.hpp"
# include <iostream>

using namespace std;

int main()//want to rename it to: new_function(); //and shift main() in 
//new main.cpp file

{
  char aObjects1 = {'A', 'B', 'C', 'D' };
  size_t aEdges1 = {0,1, 1,2, 2,3, 3,0 };
  graph::Graph<char> g1(aObjects1, 4, aEdges1, 4);
  g1.computeFundamentalCycles();
  g1.computeAllCycles();
  g1.dump(cout);
  ....

}

 2. graph.hpp // has all (public/private) data members, functions //declared here

#ifndef __GRAPH_H__
#define __GRAPH_H__

# include <algorithm>
# include <iostream>
# include <memory>
# include <vector>
# include <random>
# include <stack>
# include <list>
# include <set>
# include <map>


//stating as in C header files: 
int new_function();// is not helping

namespace graph
{
  class HalfAdjacencyMatrix
  {
     ...
  }

  template<class TObject>
  class Graph
  {
    ...
  }
  
  inline bool CreateRandomGraphIsConnected(const size_t& a, const size_t& b, const void * pEdgeProbabilty)
  {
    ...
  }
 
  std::unique_ptr<Graph<size_t> > CreateRandomGraph(size_t nNodes, float fEdgeProbability)
   {
     ...
   }

}
#endif

-----------------

3. New main.cpp file that will just call the renamed function: new_function();

int main(int argc, char** argv)
{
  new_function();
}

-----------------------------
------------------------------


 Minimal example:

1.  GraphTest.cpp //calls functions defined in graph.hpp
# include "graph.hpp"
# include <iostream>

using namespace std;

int main()//want to rename it to: new_function(); //and shift main() in 
//new main.cpp file

{
  char aObjects1 = {'A', 'B', 'C', 'D' };
  size_t aEdges1 = {0,1, 1,2, 2,3, 3,0 };
  graph::Graph<char> g1(aObjects1, 4, aEdges1, 4);
  g1.computeFundamentalCycles();
  g1.computeAllCycles();
  g1.dump(cout);
  ....

}

---------------

 2. graph.hpp // has all (public/private) data members, functions //declared here

#ifndef __GRAPH_H__
#define __GRAPH_H__

# include <algorithm>
# include <iostream>
# include <memory>
# include <vector>
# include <random>
# include <stack>
# include <list>
# include <set>
# include <map>


//stating as in C header files: 
int new_function();// is not helping

namespace graph
{
  class HalfAdjacencyMatrix
  {
     ...
  }
 .....

}
-----------------

3. New main.cpp file that will just call the renamed function: new_function();

int main(int argc, char** argv)
{
  new_function();
}

Last edited on
Your code tags are bit messed up, you can try to fix it by editing your post.

But, anyway,

char aObjects1 = {'A', 'B', 'C', 'D' }; This will give a warning in C that you should heed, and will not compile in C++. Perhaps you meant to make an array, in which case it would be
char aObjects1[] = {'A', 'B', 'C', 'D' };
If you want that null-terminated, you can just do.
char aObjects1[] = "ABCD";

Same thing with aEdges1. It's not an array.

________________________________

The other issue is I see you are separating out GraphTest.cpp and GraphTest.hpp.
Graph is a class template, so you can't separate the implementation of a template from its interface, because the compiler needs to know the full definition of the template to be able to generate the Graph<char> class.

See: https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl
Last edited on
@Ganado I am able to run ( the original code) it perfectly, in Dev-C++, and also online at: onlinegdb.com.
The only issue faced is I think based on the environment
(in Dev-C++), where needed -std=c++11.
Will specify link soon to see the 'original' code running, in both environments.

It is the creation of a new main.cpp file, that is creating issues.

Last edited on
You should make a minimal example that reproduces the error, and show exactly how you're compiling. Without seeing more, the evidence so far points towards the issue being that you are separating the template implementation from its interface.
@Ganado I recognise my fault of complicating unnecessarily. The minimal example would be as the modified Minimal example given in code.
Just note that there is no error in compilation if just two files are used, with main() in GraphTest.cpp.

Please check here: https://ibb.co/CPPKfQ3 , it is showing successful run in Dev- C++. I mean this for original two files with main() in GraphTest.cpp.

Also, can see output in onlinegdb.com at: https://ibb.co/F8FWNhL . Here, main.cpp file is mandatory and is commented out due to the stated issue (here). Also because of this, wanted to rename main() in GraphTest.cpp to new_function().
Last edited on
@Ganado I followed your advice and created files from scratch & got a different type of error in the header file.

multiple definition of `graph::CreateRandomGraph(unsigned long long, float)'

The process of creating the 3 files from scratch : main.cpp, graph.hpp, GraphTest.cpp made the stated issue vanish, and it worked same as in C, but for first few compilations.
Seems that C++ is not stable.
Last edited on
Yep, must be that C++ is not stable.
The process of creating the 3 files from scratch : main.cpp, graph.hpp, GraphTest.cpp ...


Can we see those files?

Post each file within is own set of code tags.

@Doug I meant a very simple version. My main issue is in my next comment.

Anyway, please see it

1. main.cpp

1
2
3
4
5
6
#include "graph.hpp"

int main(int argc, char** argv) {
	new_function();
	return 0;
}


2. GraphTest.cpp

1
2
3
4
5
6
//with this simple version no need to include graph.hpp
int new_function()
{
	cout<< "new_function\n";
	printf("new_function\n");
}



3. graph.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef __GRAPH_H__
#define	__GRAPH_H__

#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>
#include <random>
#include <stack>
#include <list>
#include <set>
#include <map>

int new_function();

#endif 


Last edited on
@Ganado

am redefining my problem:
If keep the main() in GraphTest.cpp, then have 2 files only and no compilation error occurs.
But, if take this outside in main.cpp, and rename the same as new_function() in GraphTest.java; then get compilation error.
/tmp/cc8M1G77.o: In function `graph::CreateRandomGraph(unsigned long, float)':
gcode.cpp:(.text+0x0): multiple definition of `graph::CreateRandomGraph(unsigned long, float)'
/tmp/ccwa4fjO.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status

Also, the error occurs even if have barebones GraphTest.java, shown below:

1
2
3
4
5
#include "graph .hpp"
int new_function()
{
    return 0 ;
}

So, just the inclusion of graph.hpp file here (GraphTest.cpp) causes the error.

In the 3 file case, has main.cpp:
1
2
3
4
5
6
#include "g.hpp"

int main(int argc, char** argv) {
	new_function();
	return 0;
}


If your statement regarding keeping the template interface and implementation in the same file is correct, then how it is that 2 file implementation works? But, 3 file iplementation doesn't. As per you only one file implementation should work.
Last edited on
Topic archived. No new replies allowed.