OccupyMars2025 / dengjunhui-data-structure

1 stars 0 forks source link

[SOLVED] error: ‘print’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation #5

Open OccupyMars2025 opened 11 months ago

OccupyMars2025 commented 11 months ago

report error:

src/Graph$ make all

occupymars2025@occupymars2025:~/Downloads/dengjunhui-data-structure/dengjunhui-data-structure-cpp-source-code/src/Graph$ make all 
mkdir -p ./build/apps
mkdir -p ./build/objects
c++ -pedantic-errors  -Wextra -Werror  -I../  -MMD -o build/objects/./Graph_test.o  -c Graph_test.cpp  
In file included from ../Graph/Graph_implementation.h:24,
                 from ../Graph/Graph.h:65,
                 from ../UniPrint/print.h:32,
                 from Graph_test.h:14,
                 from Graph_test.cpp:12:
../Graph/Graph_bcc.h: In instantiation of ‘void Graph<Tv, Te>::BCC(Rank, Rank&, Stack<int>&) [with Tv = char; Te = int; Rank = int]’:
../Graph/Graph_bcc.h:17:10:   required from ‘void Graph<Tv, Te>::bcc(Rank) [with Tv = char; Te = int; Rank = int]’
Graph_test.cpp:100:10:   required from here
../Graph/Graph_bcc.h:40:25: error: ‘print’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
   40 |                   print ( vertex ( temp.top() ) );
      |                   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from Graph_test.h:14,
                 from Graph_test.cpp:12:
../UniPrint/print.h:83:35: note: ‘template<class T> void print(const T&)’ declared here, later in the translation unit
   83 | template <typename T> static void print ( const T& x ) {  UniPrint::p ( x );  } //for Stack
      |                                   ^~~~~
In file included from ../Graph/Graph_implementation.h:24,
                 from ../Graph/Graph.h:65,
                 from ../UniPrint/print.h:32,
                 from Graph_test.h:14,
                 from Graph_test.cpp:12:
../Graph/Graph_bcc.h:42:21: error: ‘print’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
   42 |                print( vertex ( parent(u) ) );
      |                ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from Graph_test.h:14,
                 from Graph_test.cpp:12:
../UniPrint/print.h:83:35: note: ‘template<class T> void print(const T&)’ declared here, later in the translation unit
   83 | template <typename T> static void print ( const T& x ) {  UniPrint::p ( x );  } //for Stack
      |                                   ^~~~~
make: [Makefile:25: build/objects/./Graph_test.o] Error 1 (ignored)
c++ -pedantic-errors  -Wextra -Werror  -I../  -MMD -o build/objects/../UniPrint/print_basic.o  -c ../UniPrint/print_basic.cpp  
c++ -pedantic-errors  -Wextra -Werror  -I../  -MMD -o build/objects/../UniPrint/print_HuffChar.o  -c ../UniPrint/print_HuffChar.cpp  
c++ -pedantic-errors  -Wextra -Werror  -I../  -MMD -o build/objects/../UniPrint/print_int_array.o  -c ../UniPrint/print_int_array.cpp  
c++ -pedantic-errors  -Wextra -Werror  -o ./build/apps/program build/objects/./Graph_test.o build/objects/../UniPrint/print_basic.o build/objects/../UniPrint/print_HuffChar.o build/objects/../UniPrint/print_int_array.o  -L/usr/lib -lstdc++ -lm
/usr/bin/ld: cannot find build/objects/./Graph_test.o: No such file or directory
collect2: error: ld returned 1 exit status
make: [Makefile:29: build/apps/program] Error 1 (ignored)
occupymars2025@occupymars2025:~/Downloads/dengjunhui-data-structure/dengjunhui-data-structure-cpp-source-code/src/Graph$ 
OccupyMars2025 commented 11 months ago

Analysis:

study the following part carefully:

In file included from ../Graph/Graph_implementation.h:24,
                 from ../Graph/Graph.h:65,
                 from ../UniPrint/print.h:32,
                 from Graph_test.h:14,
                 from Graph_test.cpp:12:

You can find that when #include files, the print(T&) is used before declaration in Graph/Graph.h

OccupyMars2025 commented 11 months ago

Solution: (workaround ) add print(T&) declaration at the top of src/UniPrint/print.h


#pragma once

template <typename T> static void print ( const T& x );
template <typename T> static void print ( T& x );

#define PRINT(x)  { print(x); crc(x); checkOrder(x); printf("\n"); }

#include <cstdio> //采用C风格精细控制输出格式
#include "Huffman/HuffChar.h" //Huffman超字符
#include "BinTree/BinTree.h" //二叉树
#include "Huffman/HuffTree.h" //Huffman树