一个类函数的多重定义错误

问题描述 投票:0回答:1

我正在开发一个程序,使用堆栈和队列来测试用户输入的字符串是否是一个词缀。这只是一个测试,以确保我的函数正确通过。不过我一直遇到这个多重定义的错误。我试过重命名我的变量和函数,但同样的错误一直在发生。

编译器创建的Makefile。

# Project: P5
# Makefile created by Dev-C++ 5.11

CPP      = g++.exe
CC       = gcc.exe
WINDRES  = windres.exe
OBJ      = main.o Palindrome.o
LINKOBJ  = main.o Palindrome.o
LIBS     = -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc
INCS     = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS  = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
BIN      = P5.exe
CXXFLAGS = $(CXXINCS) 
CFLAGS   = $(INCS) 
RM       = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
    ${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
    $(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)

main.o: main.cpp
    $(CPP) -c main.cpp -o main.o $(CXXFLAGS)

Palindrome.o: Palindrome.c
    $(CPP) -c Palindrome.c -o Palindrome.o $(CXXFLAGS)

错误信息:

C:\Users\me\Downloads\school\CSC\260\Palindrome.o   Palindrome.c:(.text+0x0): multiple definition of `TestPal::isPalindrome(std::string)'
C:\Users\me\Downloads\school\CSC\260\main.o main.cpp:(.text+0x0): first defined here
C:\Users\me\Downloads\school\CSC\260\Palindrome.o   Palindrome.c:(.text+0x2e): multiple definition of `TestPal::TestPal()'
C:\Users\me\Downloads\school\CSC\260\main.o main.cpp:(.text+0x2e): first defined here
C:\Users\me\Downloads\school\CSC\260\Palindrome.o   Palindrome.c:(.text+0x2e): multiple definition of `TestPal::TestPal()'
C:\Users\me\Downloads\school\CSC\260\main.o main.cpp:(.text+0x2e): first defined here
C:\Users\me\Downloads\school\CSC\260\collect2.exe   [Error] ld returned 1 exit status
25      C:\Users\me\Downloads\school\CSC\260\Makefile.win   recipe for target 'P5.exe' failed

Main.Palindrome.h

#include <iostream>
#include "stack"
#include "queue"
#include "Palindrome.c"
using namespace std;

int main()
{
    string Pal;
    cout << "Enter a palindrome: ";
    getline (cin, Pal);

    TestPal test;
    test.isPalindrome(Pal);


    return 0;
}

Palindrome.h

#ifndef PALINDROME_H
#define PALINDROME_H
#include <iostream>
#include <string>
using namespace std;

class TestPal
{
    public: 
        bool isPalindrome(string);
        TestPal();

};

#endif

Palindrome.c

#include "Palindrome.h"
#include "stack"
#include "queue"
#include <iostream>
#include <string>
using namespace std;

bool TestPal::isPalindrome(string Pal)
{

    cout << Pal;
    return false;
};

TestPal::TestPal()
{

};
c++ class makefile
1个回答
2
投票

这一行 #include "Palindrome.c" 是问题所在,你在导入实现,这对编译来说是个大问题。

这应该是。

#include "Palindrome.h"

除非你正在做一些非常奇怪的事情,否则永远不要包含... ... .c 文件。坚持 .h (或 .hpp 如果你愿意的话)。

© www.soinside.com 2019 - 2024. All rights reserved.