C++ 构造函数的多重声明
Multiple declarations of a constructor in C++
我正在为一个 class 做作业,我正在 运行 解决(我认为是)一个基本问题我已经尝试查看其他答案,但我似乎找不到我的问题.
当我 运行 命令 make
时,出现以下错误:
prog8lib.o: In function `Transform':
.../prog8lib.cc:6: multiple definition of `Transform::Transform(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
prog8.o:.../prog8lib.cc:6: first defined here
prog8lib.o: In function `Transform':
.../prog8lib.cc:6: multiple definition of `Transform::Transform(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
prog8.o:.../prog8lib.cc:6: first defined here
这是我目前使用的代码。谁能发现我的错误?
谢谢!!!
prog8.cc:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "prog8lib.cc"
using namespace std;
int main(int argc, char** argv) {
string start = argv[1];
string end = argv[2];
Transform(start, end);
}
prog8lib.cc:
#include <iostream>
#include <string>
#include <stdlib.h>
#include "prog8lib.h"
Transform::Transform(string startWord, string endWord) {
cout << "Test" << endl;
}
Transform::~Transform() {}
prog8lib.h:
#ifndef PROG8LIB_H
#define PROG8LIB_H
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Transform{
private:
string start;
string end;
public:
Transform(string str, string str2);
~Transform();
};
#endif
生成文件:
OBJ = prog8.o prog8lib.o
OPTS = -g -c -Wall -Werror
trans: $(OBJ)
g++ -o trans $(OBJ)
prog8.o: prog8.cc
g++ $(OPTS) prog8.cc
prog8lib.o: prog8lib.cc prog8lib.h
g++ $(OPTS) prog8lib.cc
clean:
rm -f *.o *~
我正在为一个 class 做作业,我正在 运行 解决(我认为是)一个基本问题我已经尝试查看其他答案,但我似乎找不到我的问题.
当我 运行 命令 make
时,出现以下错误:
prog8lib.o: In function `Transform':
.../prog8lib.cc:6: multiple definition of `Transform::Transform(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
prog8.o:.../prog8lib.cc:6: first defined here
prog8lib.o: In function `Transform':
.../prog8lib.cc:6: multiple definition of `Transform::Transform(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
prog8.o:.../prog8lib.cc:6: first defined here
这是我目前使用的代码。谁能发现我的错误?
谢谢!!!
prog8.cc:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "prog8lib.cc"
using namespace std;
int main(int argc, char** argv) {
string start = argv[1];
string end = argv[2];
Transform(start, end);
}
prog8lib.cc:
#include <iostream>
#include <string>
#include <stdlib.h>
#include "prog8lib.h"
Transform::Transform(string startWord, string endWord) {
cout << "Test" << endl;
}
Transform::~Transform() {}
prog8lib.h:
#ifndef PROG8LIB_H
#define PROG8LIB_H
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Transform{
private:
string start;
string end;
public:
Transform(string str, string str2);
~Transform();
};
#endif
生成文件:
OBJ = prog8.o prog8lib.o
OPTS = -g -c -Wall -Werror
trans: $(OBJ)
g++ -o trans $(OBJ)
prog8.o: prog8.cc
g++ $(OPTS) prog8.cc
prog8lib.o: prog8lib.cc prog8lib.h
g++ $(OPTS) prog8lib.cc
clean:
rm -f *.o *~