.cpp文件中函数的多个定义

时间:2012-11-06 03:15:40

标签: c++ function include declaration

编辑:

叹了口气,我觉得自己像个白痴。在我问他的问题几个月后,我从来没有正确地向我解释c ++文件包含。请不要评论我的设置究竟有多严重,我完全清楚。

我有一个包含许多.h和.cpp文件的多文件项目。尤其是io_util.h,会产生一系列奇怪的错误。对于包含函数声明但没有实现的.h,编译完美,但在函数体所在的.cpp中,每个函数都会产生以下错误:

   Multiple markers at this line
- first defined here
- multiple definition of 
 `<namespace>::<function name>(<args>)'

文件“io_util.cpp”在项目CollectionBase.h中只包含一次。 “io_util.h”仅包含在“io_util.cpp”

以下是两个文件:

·H:

/*
 * io_util.h
 */

#ifndef IO_UTIL_H_
#define IO_UTIL_H_

#include <iostream>
#include <sstream>
#include <string>
#include <cmath>

#define IO_DEBUG(a) cout << #a << '=' << a << '\n'

#define CASE_SEP 32
#define NUM_CHAR_SEP 48

namespace std
{
    void getVar(int&, int);

    void getVar(double&, double);

    void getVar(unsigned&, unsigned);

    int get_num_digits(int);

    int get_digit(int, const int, const int);

    string value_of(int);
}

#endif /* IO_UTIL_H_ */

的.cpp:

/*
 * io_util.cpp
 */
#ifndef IO_H_
#define IO_H_

#include "io_util.h"

namespace std
{

void getVar(int& i, int forbidden = NAN)
{
    string str;

    while(true)
    {
        getline(cin,str);

        if(str.find_first_not_of("-0123456789") != string::npos || !(stringstream(str) >> i))
        {
            cout << "invalid input.\n\n";
        }
        else if(i == forbidden)
        {
            cout << "illegal value.\n\n";
        }
        else
        {
            break;
        }
    }
}

void getVar(double& d, double forbidden = NAN)
{
    string str;

    while(true)
    {
        getline(cin,str);

        if(str.find_first_not_of("-.0123456789eE") != string::npos || !(stringstream(str) >> d))
        {
            cout << "invalid input.\n\n";
        }
        else if(d == forbidden)
        {
            cout << "illegal value.\n\n";
        }
        else
        {
            break;
        }
    }
}

void getVar(unsigned & u, unsigned forbidden = NAN)
{
    string str;

    while(true)
    {
        getline(cin,str);

        if(str.find_first_not_of("0123456789") != string::npos || !(stringstream(str) >> u))
        {
            cout << "invalid input.\n\n";
        }
        else if(u == forbidden)
        {
            cout << "illegal value.\n\n";
        }
        else
        {
            break;
        }
    }
}

int get_num_digits(int i)
{
    if(i < 0)
    {
        i = -i;
    }

    int result = 1;

    while(i > 10)
    {
        i /= 10;
        result++;
    }

    return result;
}

int get_digit(int i, const int num_digits, const int dig)
{
    if(num_digits < dig)
    {
        return 0;
    }

    const int dig_p10 = pow(10.0,num_digits - dig);

    i -= dig_p10 * round(i/dig_p10);

    if (dig < num_digits - 1)
    {
        i = round(i/int(pow(10.0,num_digits - dig - 1)));
    }

    return i;
}

string value_of(int i)
{
    string str;

    if (i < 0)
    {
        str += "-";
        i = -i;
    }

    const int num_dig = get_num_digits(i);

    for(int n = 0; n < num_dig; n++)
    {
        str += char(get_digit(i, num_dig, n) + NUM_CHAR_SEP);
    }

    return str;
}

}

#endif /*IO_H_*/

我在本网站上查看了很多关于类似现象的问题,但没有一个问题有帮助。

1 个答案:

答案 0 :(得分:1)

首先,io_util.cpp应该在要编译的代码文件的 source 文件列表中。项目中需要其中包含的功能的任何地方都应该包括io_util.h,包括CollectionBase.h。在任何 #include .cpp文件(即不允许#include“io_util.cpp”)。

其次,你的io_util.h头部在std命名空间(也称为no-no)中声明了名为getVar()的函数。尾随参数的默认参数值属于那里; io_util.cpp文件中

修复这两个问题应该会让你在未来的路上走得更远。