C ++关于.h和.cpp - 的多重定义

时间:2012-07-28 09:39:33

标签: c++

我遇到了“多重定义”的错误 我有3个文件,即currency.h,currency.cpp,main.cpp

在currencyConverter类下的currencyConverter.h 我做了

using namespace std;

class currencyConverter
{

string result;
stringstream ss;
size_t found,found2;

public:
   void getInbetween(string,string);

};

#endif  /* CURRENCYCONVERTER_H */

然后在currencyConverter.cpp我做了

#include "currencyConverter.h"
void currencyConverter::getInbetween(string selection,string str2,string str3,string sdata)
{

buffer[result.length()] = '\0';     //insert '\0'

char * pch;
 pch = strtok (buffer," ");

}

void currencyConverter::webparser(const string siteurl,const string filename)
{
    ss << "lynx -dump '" << siteurl << "' > " << filename;
}

string currencyConverter::userOption()
{
    //some code    
    return selection;
}

at main2.cpp

#include<iostream>


#include"currencyConverter.cpp"

using namespace std;

int main() {

    currencyConverter c;
    string exitstr;


    if(selection!="6")
    {

    c.webparser(parsePage,"file.txt");    
    //now perform searchstring
    c.searchString(selection,"file.txt");
    }


    }while (1);

return 0;
}

这是我的make文件

# ExampleTests Project

SRCS = main2.cpp

HDRS = 

PROJ = main

CC = g++

OBJS = $(SRCS:.cpp=.o)

APP = $(PROJ).exe

CFLAGS = -c -g -Wall -I/opt/local/include

ifeq (,$(findstring CYGWIN,$(shell uname)))

LIBS = -lcppunit -ldl



all: $(APP)



$(APP): $(OBJS)

    $(CC) $(LDFLAGS) $(OBJS) -o $(APP) $(LIBS)




clean:

    rm -f *.o $(APP)

但我在编译器上收到此错误

如何解决这个问题..我没有使用它两次。我使用函数webparser粘贴了我的currencyConverter.cpp的一部分,我在main2.cpp调用函数的方式是否有错误?

rm -f *.o main.exe

CLEAN SUCCESSFUL (total time: 86ms)
g++    -c -o main2.o main2.cpp

td :: char_traits,std :: allocator&gt;)':     currencyConverter.cpp :(。text + 0xcec):`currencyConverter :: webparser(std :: basic_string,std :: allocator&gt;,std :: basic_string,std :: allocator&gt;)'的多重定义     main2.o:main2.cpp :(。text + 0xcec):首先在这里定义     collect2:ld返回1退出状态     make: * [main.exe]错误1

1 个答案:

答案 0 :(得分:3)

包含头文件,而不是cpp

#include"currencyConverter.cpp"

应该是

#include"currencyConverter.h"

main2.cpp