Error unterminated ifndef

Hi,I spent all night trying to figure this out. Could someone take a look and show me what I'm missing? I getting this compiler error: [B]unterminated ...


Recommended Answers

Make sure there is a carriage return at the end of the last line.
Some compilers are picky.

Jump to Post

Hi,I spent all night trying to figure this out. Could someone take a look and show me what I’m missing? I getting this compiler error: unterminated #ifndef . But I have ifndef at the beginning and endif at the end of the class.

//Header file. 
//Class definition for …

Jump to Post

All 5 Replies

Member Avatar


thines01

401



Postaholic



Team Colleague



Featured Poster


11 Years Ago

Make sure there is a carriage return at the end of the last line.
Some compilers are picky.

Member Avatar

11 Years Ago

Hi,I spent all night trying to figure this out. Could someone take a look and show me what I’m missing? I getting this compiler error: unterminated #ifndef . But I have ifndef at the beginning and endif at the end of the class.

//Header file. 
//Class definition for teh stack ADT

#ifndef _mystack_H
#include <ostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define _mystack_H


const unsigned int maxSize = 10;

class Stack
{
      public:
                Stack(); //constructor
      
                ~Stack(); //Destructor

                bool isEmptyStack();
                
                bool isFullStack();
                
                void pushStack(int newItem);
                
                void popStack(int item);
                
                void initializeStack();
                
                void fillStack(int numSize);
                
                void exchangeTopAndBottom(Stack &stk);
                
                void printStack(Stack &stk);
                
                int sumStack(Stack &stk);
                
                void OddElem(Stack &stk);
                
                //void commonStack(Stack &stk1, Stack &stk2);
                
                void intersectStack(Stack &stk1, Stack &stk2);

       private:
                int maxSize;  //variable to store the maximum stack size
                int stackTop; //variable to poit to the top of the stack
                Stack arrList;//pointer to the array that holds the stack 
                              //elements
        
};      

#endif

First of all, «Stack arrList» —this is not legal as far as I know in C++. This should be Stack* arrlist. Fix this first and try it.

I’m not sure about your #ifndef problem. I copy/pasted your code and it compiled after I fixed the Stack*. My guess is that it’s one of your overlapping includes that’s not being properly handled by your particular compiler.

fstream automatically includes ostream. Here’s a map of how that works:
http://www.cplusplus.com/reference/iostream/
Anything that an arrow points to, will automatically include its parent class, tracing backwards. So you only need to include fstream. First remove #include <ostream> and try to compile. Pretty sure stdio automatically includes stdlib, too. You should try to remove one or the other. Stdio is C input output. In my opinion you shouldn’t use both the C++ streams library and the Stdio library, as they both perform similar tasks.

You should #include as few things as possible. Not only does it bloat your code, but it can confuse your compiler as well.

Edited

11 Years Ago
by Greywolf333 because:

n/a

Member Avatar


LevyDee

2



Posting Whiz in Training


11 Years Ago

You need to format your defines as such:
#ifndef
#define

#endif

after you said #ifndef _mystack_H
you need to then put a #define right after that on line 5

Member Avatar


murnesty

0



Junior Poster in Training


11 Years Ago

It may caused by previous header file

Member Avatar


BoBok2002

0



Junior Poster in Training


11 Years Ago

It worked! I repositioned the ifndef, define, and endif. Then I changed #include <ostream> to #include <iostream> and I compiled the code on a different compiler, and it worked. I fixed the Stack *arrList too.

Now I’m getting another error. The last one I hope. It says

[Linker error] undefined reference to `Stack::Stack()’

And there is a linker error displayed for all the functions in my implementation file. Also, #include «mystack.h» has been highlighted in both the main and implementation file. Please help me with this one too.
Thanks.


Reply to this topic

Be a part of the DaniWeb community

We’re a friendly, industry-focused community of developers, IT pros, digital marketers,
and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Reading time: 20 minutes | Coding time: 5 minutes

ifndef is a include guard. It is a define directive in C which checks if a MACRO has been defined previously or not. If it has not been defined, it will define the MACROs and other statements. It is used for conditional compilation.

ifndef can be seen as «if not defined» then do this till endif.

Syntax:

#ifndef <macro_definition>
... some code statements
#endif

It should always end with endif.

Example

In this example, we see a simple use of ifndef for defining a macro named DATA in a C code. As DATA macro is not defined, it will be defined now.

#include <stdio.h>

#ifndef DATA
#define DATA "opengenus" 
#endif

int main()
{
   printf(DATA);
   return 0;
}

Compile it as:

gcc opengenus.c

Output:

opengenus

If we do not end with endif, we will get a compile time error as:

opengenus.c:3:0: error: unterminated #ifndef
 #ifndef DATA
 ^

We can define a global variable in the ifndef block as well like:

#include <stdio.h>

#ifndef DATA
#define DATA "opengenus" 
int global_i = 5;
#endif

int main()
{
   printf("%sn", DATA);
   printf("%d", global_i);
   return 0;
}

Output:

opengenus
5

In case, the macro DATA is defined previously, then the two code statements of defining DATA macro and global_i variable will not be defined.

For example:

#include <stdio.h>

#define DATA "og"

#ifndef DATA
#define DATA "opengenus" 
int global_i = 5;
#endif

int main()
{
   printf("%sn", DATA);
   printf("%d", global_i);
   return 0;
}

This will throw a compilation error as global_i will not be defined.
Error:

opengenus.c: In function 'main':
opengenus.c:13:17: error: 'global_i' undeclared (first use in this function)
    printf("%d", global_i);
                 ^
opengenus.c:13:17: note: each undeclared identifier is reported only once for each function it appears in

Let us fix the code:

#include <stdio.h>

#define DATA "og"

#ifndef DATA
#define DATA "opengenus" 
#endif

int main()
{
   printf("%sn", DATA);
   return 0;
}

Output:

og

The output is og as DATA is defined as og and the second define function is not reached as we are using ifndef.

Consider a header file sodium.h

#ifndef MIXTURE_H
#define MIXTURE_H

struct mineral {
    int saturation;
};

#endif /* MIXTURE_H */

Following it, we have another file say salt.h as:

#ifndef MIXTURE_H
#define MIXTURE_H

struct mineral {
    int saturation;
};struct mineral {
    int saturation;
};

#endif /* MIXTURE_H */

Now in our water.c file, we may either sodium.h or salt.h or both.

#include "salt.h"
#include "sodium.h"

As both sodium.h and salt.h is included and both are using ifndef, there is no problem for the duplicate structure. If we had not used ifndef, there this situation will give an error.

The file water.c will be evaluated as:

struct mineral {
    int saturation;
};

Application

ifndef is widely used as in large codebases, it may not be possible to track.

If we redefine a macro, it will give a compilation error. Consider the following code where we will define the DATA macro twice.

#include <stdio.h>

#define DATA "og"

#define DATA "opengenus" 

int main()
{
   printf("%sn", DATA);
   return 0;
}

Error:

opengenus.c:5:0: warning: "DATA" redefined
 #define DATA "opengenus" 
 ^
opengenus.c:3:0: note: this is the location of the previous definition
 #define DATA "og"
 ^

MACROs are platform specific as specific MACROs are defined only if certain conditions are satisfied like:

  • operating systems
  • runtime libraries

and others.

Hence, in such scenarios, a code may compile on a certain environment but it will fail in other environment. Hence, to ensure that your code does not fail in any scenario, use ifndef.

$ ldd build/release/bin/cquery 
	linux-vdso.so.1 (0x00007ffe07781000)
	libatomic.so.1 => /usr/lib64/libatomic.so.1 (0x00007f4ed585e000)
	libpthread.so.0 => /usr/lib64/libpthread.so.0 (0x00007f4ed563f000)
	libdl.so.2 => /usr/lib64/libdl.so.2 (0x00007f4ed543b000)
	libclang.so.5 => /usr/lib64/libclang.so.5 (0x00007f4ed51b6000)
	libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f4ed4e2f000)
	libm.so.6 => /usr/lib64/libm.so.6 (0x00007f4ed4ada000)
	libgcc_s.so.1 => /usr/lib64/libgcc_s.so.1 (0x00007f4ed48c3000)
	libc.so.6 => /usr/lib64/libc.so.6 (0x00007f4ed44e0000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f4ed5a66000)
	libclangAST.so.5 => /usr/lib64/../lib64/libclangAST.so.5 (0x00007f4ed3f7e000)
	libclangBasic.so.5 => /usr/lib64/../lib64/libclangBasic.so.5 (0x00007f4ed3aad000)
	libclangFrontend.so.5 => /usr/lib64/../lib64/libclangFrontend.so.5 (0x00007f4ed3753000)
	libclangIndex.so.5 => /usr/lib64/../lib64/libclangIndex.so.5 (0x00007f4ed3509000)
	libclangLex.so.5 => /usr/lib64/../lib64/libclangLex.so.5 (0x00007f4ed3241000)
	libclangSema.so.5 => /usr/lib64/../lib64/libclangSema.so.5 (0x00007f4ed2945000)
	libclangTooling.so.5 => /usr/lib64/../lib64/libclangTooling.so.5 (0x00007f4ed26db000)
	libclangARCMigrate.so.5 => /usr/lib64/../lib64/libclangARCMigrate.so.5 (0x00007f4ed228c000)
	libclangTidyPlugin.so.5 => /usr/lib64/../lib64/libclangTidyPlugin.so.5 (0x00007f4ed2054000)
	libclangIncludeFixerPlugin.so.5 => /usr/lib64/../lib64/libclangIncludeFixerPlugin.so.5 (0x00007f4ed1e46000)
	libLLVM-5.0.so => /usr/lib64/../lib64/libLLVM-5.0.so (0x00007f4ece947000)
	libclangDriver.so.5 => /usr/lib64/../lib64/../lib64/libclangDriver.so.5 (0x00007f4ece5b0000)
	libclangEdit.so.5 => /usr/lib64/../lib64/../lib64/libclangEdit.so.5 (0x00007f4ece3a1000)
	libclangParse.so.5 => /usr/lib64/../lib64/../lib64/libclangParse.so.5 (0x00007f4ece0a3000)
	libclangSerialization.so.5 => /usr/lib64/../lib64/../lib64/libclangSerialization.so.5 (0x00007f4ecdd18000)
	libclangFormat.so.5 => /usr/lib64/../lib64/../lib64/libclangFormat.so.5 (0x00007f4ecda95000)
	libclangRewrite.so.5 => /usr/lib64/../lib64/../lib64/libclangRewrite.so.5 (0x00007f4ecd884000)
	libclangToolingCore.so.5 => /usr/lib64/../lib64/../lib64/libclangToolingCore.so.5 (0x00007f4ecd671000)
	libclangAnalysis.so.5 => /usr/lib64/../lib64/../lib64/libclangAnalysis.so.5 (0x00007f4ecd3cb000)
	libclangASTMatchers.so.5 => /usr/lib64/../lib64/../lib64/libclangASTMatchers.so.5 (0x00007f4ecd151000)
	libclangStaticAnalyzerCheckers.so.5 => /usr/lib64/../lib64/../lib64/libclangStaticAnalyzerCheckers.so.5 (0x00007f4eccc08000)
	libclangTidy.so.5 => /usr/lib64/../lib64/../lib64/libclangTidy.so.5 (0x00007f4ecc997000)
	libclangTidyAndroidModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyAndroidModule.so.5 (0x00007f4ecc754000)
	libclangTidyBoostModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyBoostModule.so.5 (0x00007f4ecc516000)
	libclangTidyCERTModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyCERTModule.so.5 (0x00007f4ecc2b6000)
	libclangTidyCppCoreGuidelinesModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyCppCoreGuidelinesModule.so.5 (0x00007f4ecc02f000)
	libclangTidyGoogleModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyGoogleModule.so.5 (0x00007f4ecbdc7000)
	libclangTidyLLVMModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyLLVMModule.so.5 (0x00007f4ecbb86000)
	libclangTidyMiscModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyMiscModule.so.5 (0x00007f4ecb803000)
	libclangTidyModernizeModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyModernizeModule.so.5 (0x00007f4ecb3f4000)
	libclangTidyMPIModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyMPIModule.so.5 (0x00007f4ecb1b5000)
	libclangTidyPerformanceModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyPerformanceModule.so.5 (0x00007f4ecaf38000)
	libclangTidyReadabilityModule.so.5 => /usr/lib64/../lib64/../lib64/libclangTidyReadabilityModule.so.5 (0x00007f4ecac11000)
	libclangIncludeFixer.so.5 => /usr/lib64/../lib64/../lib64/libclangIncludeFixer.so.5 (0x00007f4eca9e2000)
	libffi.so.6 => /usr/lib64/../lib64/../lib64/libffi.so.6 (0x00007f4eca7da000)
	libedit.so.0 => /usr/lib64/../lib64/../lib64/libedit.so.0 (0x00007f4eca5a3000)
	librt.so.1 => /usr/lib64/../lib64/../lib64/librt.so.1 (0x00007f4eca39b000)
	libtinfo.so.6 => /usr/lib64/../lib64/../lib64/libtinfo.so.6 (0x00007f4eca16f000)
	libz.so.1 => /usr/lib64/../lib64/../lib64/libz.so.1 (0x00007f4ec9f58000)
	libclangStaticAnalyzerCore.so.5 => /usr/lib64/../lib64/../lib64/../lib64/libclangStaticAnalyzerCore.so.5 (0x00007f4ec9be3000)
	libclangStaticAnalyzerFrontend.so.5 => /usr/lib64/../lib64/../lib64/../lib64/libclangStaticAnalyzerFrontend.so.5 (0x00007f4ec99a6000)
	libclangTidyUtils.so.5 => /usr/lib64/../lib64/../lib64/../lib64/libclangTidyUtils.so.5 (0x00007f4ec973a000)
	libfindAllSymbols.so.5 => /usr/lib64/../lib64/../lib64/../lib64/libfindAllSymbols.so.5 (0x00007f4ec94ce000)
	libncurses.so.6 => /usr/lib64/../lib64/../lib64/libncurses.so.6 (0x00007f4ec92a5000)

Those come from the package clang-libs-5.0.1-3.fc27.x86_64.

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Error unsupported source watch
  • Error while waiting for mnesia tables
  • Error unsupported pyinstaller version or not a pyinstaller archive
  • Error while waiting for device timed out after 300seconds waiting for emulator to come online
  • Error while trying to retrieve text for error ora 01019 oracle

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии