Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:04:08+00:00 2026-06-09T09:04:08+00:00

I have the following 3 files: error.h #ifndef error_h #define error_h #include <string> #include

  • 0

I have the following 3 files:

error.h

#ifndef error_h
#define error_h
#include <string>
#include <iostream>
#include <cstdio>
void Error(std::string msg);
#endif

error.cpp

#ifdef error_h
#include "error.h"
void Error(std::string msg)
{
    std::cerr
     << "\n=========================================================\n"
     << msg
     << "\n=========================================================\n";
    exit(EXIT_FAILURE);
}
#endif

foobar.cpp

#include "error.h"
int main()
{
    for(int i=0; i<99; i++)
        if(i==55)
            Error("this works");
    return 0;
}

Now I do:

$ g++ -c error.cpp foobar.cpp
$ g++ error.o foobar.o -o exampleprogram

And I get:

foobar.o: In function `main':
foobar.cpp:(.text+0x4b): undefined reference to `Error(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status

What am I doing wrong? What do I need to understand to resolve this, and similar issues in the future without asking questions? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:04:10+00:00Added an answer on June 9, 2026 at 9:04 am

    Why do you have these lines in error.cpp?

    #ifdef error_h
      ...
    #endif
    

    Since the preprocessor symbol error_h is not defined the entire contents of error.cpp are being omitted by the preprocessor. Remove those lines and your program will link successfully.

    You seem to have a misunderstanding of how (and maybe why) #include guards are to be used. Refer to this answer for an explanation.

    Also, there’s no need to include iostream and cstdio in error.h, since that file is not using anything declared in either of those headers. Those files should be included in error.cpp.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run the following code... #ifndef KEYEDITEM_H_INCLUDED #define KEYEDITEM_H_INCLUDED #include <string> typedef std::string
I have the following header file: #ifndef DATABASE_H #define DATABASE_H #include <vector> #include <iostream>
I have the following pieces of code: Leomedia.h #include MusicMetaDatter.h #ifndef LEOMEDIA_H #define LEOMEDIA_H
I have the following code in a header file: #ifndef BUFFER_H #define BUFFER_H #include
If I have the following files, I get this error (c2593 in VC9). If
I have several files which is causing a compile error, can the following be
I have the following three files in the same directory: citysim.cpp #include utils.h using
I have the following header file: #ifndef CLASSES_H #define CLASSES_H class Mouse // Handles
I have the following 4 files: arrayListType.h : Declare and define arrayListType class as
I have this abstract base class ODESolver: //ODESolver.h #ifndef ODESolver_H #define ODESolver_H #include doublependulum.h

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.