Thread: error C2084

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    error C2084

    Hi, I am building a program in Visual C++ and I get following error:

    ..\..\basic.cpp(5) : error C2084: function 'int Log(char [])' already has a body
    c:\visualcpp\myprogram\basic.h(15) : see previous definition of 'Log'

    The main file includes basic.h.


    basic.h:
    Code:
    #ifndef BASIC_H
    #define BASIC_H
    #pragma once
    
    #define WIN32_LEAN_AND_MEAN
    
    #include <stdio.h>
    
    int Log(char message[]);
    #endif

    basic.cpp:
    Code:
    #include "basic.h"
    
    
    int Log(char message[])   {
    
        char log_name []= "log.txt";
        FILE *fp;
        
        fp=fopen(log_name,"a");
        fprintf(fp,"%s\n",message);
        fclose(fp);
    
    
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... basic.h only has 10 lines? The error is reported as being on line 15.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Quote Originally Posted by laserlight
    hmm... basic.h only has 10 lines? The error is reported as being on line 15.
    That's weird. Are they really lines? I checked and there were really only 10 lines in basic.h. There are actually a bit more lines in basic.cpp. There are a few comments.

    The original basic.cpp is here:
    Code:
    #include "basic.h"
    
    
    int Log(char message[])   {
    //
    //comments
    //
    //
    
        char log_name []= "log.txt";
        FILE *fp;
        
        fp=fopen(log_name,"a");
        fprintf(fp,"%s\n",message);
        fclose(fp);
    
    
    	return 0;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Are you using precompiled headers?
    maybe clean and rebuild all will help?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    I tried, that doesn't help.

    I figured out, where that line 15 comes. It counts lines in basic.h until the line "int Log(char message[]);" and then counts lines forward from file basic.cpp function Log until the line
    "int Log(char message[]); {"

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    "int Log(char message[]); {"

    get rid of the semi colon in that line and you should be okay.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Quote Originally Posted by stinkipete
    "int Log(char message[]); {"

    get rid of the semi colon in that line and you should be okay.
    Sorry, I have written wrong. Where is no semi-colon.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need to copy / paste exactly what is in those two files without any editing to make it "pretty" or "short" for the board.

    And make sure the error messages you post are consistent with the code you post.

    Also, try a different name. It could be that Log is already defined in a standard header. It is after all a pretty common name used for both "logging" and "mathematics".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed