C++ 同一行上有多个定义错误。(C+;+;)

C++ 同一行上有多个定义错误。(C+;+;),c++,C++,我有一个新的复杂问题。编译器抱怨我正在重新定义一个函数,但它说我声明它的第一个位置是重新声明的位置。当我在另一个文件中包含cpp文件时,问题就开始了。为了解决我的问题,我将它导出到一个hpp文件中,但不知道是否有用。这是我的密码 main.cpp: #include <iostream> #include <string> #include "main.hpp" using namespace std; int main(int argc, char *argv[])

我有一个新的复杂问题。编译器抱怨我正在重新定义一个函数,但它说我声明它的第一个位置是重新声明的位置。当我在另一个文件中包含cpp文件时,问题就开始了。为了解决我的问题,我将它导出到一个hpp文件中,但不知道是否有用。这是我的密码

main.cpp:

#include <iostream>
#include <string>
#include "main.hpp"

using namespace std;

int main(int argc, char *argv[])
{
//Deal with arguments and send them to the correct functions
if (argc >= 2){

string op = argv[1];
if (op == "-a" || op == "--automatic"){
    if (argc >= 3){
        string FName = argv[2];
        bool dbgbool;
        if (argc == 4){
            string dbgstring = argv[3];
            if (dbgstring == "debug"){
                dbgbool = true;
            }
        }
        Lexer(FName, dbgbool);
    }
}
else{
    cout << "Invalid Argument\n";
    goto help;
}

return 0;
}
//Or, just write help and info
help:
cout << "\n";
cout << "bwc v0.0.1U-(Unstable)\n\n";
cout << "Usage: bwc <operation> [...]\n";
cout << "Operations:\n";
cout << "   bwc {-a --automatic} <file(s)>\n";
cout << "   bwc {-i --interactive}\n";
cout << "   bwc {-c --error-codes}\n";
cout << "\n";
return 0;
}
谢谢,
Brooks Rady

您的
main.cpp
包括
main.hpp
,它包括
LA.cpp
,因此
LA.cpp
的内容将为
LA.cpp
编译一次,为
main.cpp
编译一次

.hpp文件应该只包含声明(
string Lexer(string,bool);
),而定义(
string Lexer(string,bool){…}
)应该放在.cpp中


在处理类方法时,您不会看到这种问题,因为编译器接受方法的定义。但是函数应该只在.cpp文件中定义。

永远不要在任何地方包含.cpp文件-您应该只包含标题。。。。别忘了是的,我知道,但是,我没有把它包括在main.cpp中。我在头文件中使用了extern,有什么我做错了吗?请看我的代码。我没有把它包括在主代码中。cpp:是的,你有。我做错什么了吗?:参考安德烈的回答。不要在标题中包含cpp文件。
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>

using namespace std;

string Lexer(string FileN, bool dbg){ //This is the line of re-declaration.

//If debugging,this writes out put to the console
if (dbg == true)
    cout << "Beginning Lexical Analysis...\n";

//Create new file stream and set it equal to the source file
ifstream Ifile (FileN.c_str());
//Test if the last step failed, if so, write an error to the console, and terminate the compiler
if (!Ifile.is_open()){
    cout << "Unable to open file. Path to file may not exist, or the file name could be incorrect.\n";
    cout << "Error Code: -1\n";
    return NULL;}
//Create new stringstream, and set it equal to the source file
string IFStream;
Ifile >> IFStream;
//Close the source file
Ifile.close();

//If debugging,this writes out put to the console
if (dbg == true)
    cout << "Source file sucessfully read.\n";

//Set out stream equal to the modified in stream
string OFStream = IFStream;
return OFStream;
}
#ifndef MAIN_HPP_INCLUDED
#define MAIN_HPP_INCLUDED

#include "LA.cpp"
extern string Lexer(string,bool);

#endif // MAIN_HPP_INCLUDED