gpt4 book ai didi

c++ - 类函数的多定义错误

转载 作者:行者123 更新时间:2023-12-02 10:03:29 25 4
gpt4 key购买 nike

我正在开发一个程序,以测试用户输入的字符串是否是回文,是否使用堆栈和队列。这只是确保我的功能正确传递的测试。我仍然保留此多定义错误。我尝试重命名变量和函数,但相同的错误不断发生。

由编译器创建的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

主要:
#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;
}

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

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

};

#endif

回文
#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()
{

};

最佳答案

#include "Palindrome.c"行就是问题所在,您正在导入实现,就编译而言,这是一个巨大的问题。

应该是:

#include "Palindrome.h"

除非您做的事情确实很奇怪,否则切勿包含 .c文件。坚持使用 .h(如果愿意,也可以遵循 .hpp)。

关于c++ - 类函数的多定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61379948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com