错误LNK2001:未解析的外部符号_Main

时间:2015-12-04 20:58:45

标签: c++ visual-studio visual-c++ lnk2001

我在这里有一些代码似乎没有正确链接。我搜索过,有几个地方已经建议使用int main()来解决它。不确定我的问题是什么。我对编程很陌生,我尝试了一些不同的东西。任何帮助都会很棒!

我有四个文件:Wire.h,Wire.cpp,Gate.h和Gate.cpp。

这是Wire.h

#ifndef WIRE_H
#define WIRE_H

#include <iostream>
#include<vector>
#include<map>
#include<string>
#include "Gate.h"

using namespace std;

class Gate;

class Wire {
public:
    // constructors
    Wire();

    // destructor
    ~Wire();

    //functions
    int getState();
    void setState(int s);

private:
    int State;
    vector<Gate*> Gates;
    vector<int> History;


};

#endif //WIRE_H

这是Wire.cpp:

#include "Wire.h"

#include<iostream>
using namespace std;

int main() {
    cout << "Hello World";
    return 0;
}

Wire::Wire(){
    State = UNKNOWN;
}

Wire::~Wire(){

    for (int i = 0; i < 1/*Gates.size()*/; i++){
        Gates.pop_back();
    }
    for (int i = 0; i < 1/*History.size()*/; i++){
        History.pop_back();
    }
}

int Wire::getState() {
    return State;
}

void Wire::setState(int s) {
    State = s;
}

这是Gate.h:

#ifndef GATE_H
#define GATE_H

#include "Wire.h"

#include <iostream>
#include<vector>
#include<map>
#include<string>

using namespace std;
const int HI = 1;
const int LOW = 0;
const int UNKNOWN = -1;

class Wire;

class Gate {
public:

    // destructor
    ~Gate();

    //functions
    void logic();
    void setType(string);
    void setDelay(int);
    void setAIO(int i, int o); //Only for the NOT gate
    void setBIO(int ain, int bin, int o); //For all gates except for NOT


private:
    string Type;
    Wire* inputA;
    Wire* inputB;
    Wire* output;
    int delay;


};

#endif //GATE_H

这是Gate.cpp

#include "Gate.h"

#include<iostream>
using namespace std;

Gate::Gate(){
    inputA = new Wire();
}

Gate::~Gate(){
    delete inputA;
    delete inputB;
    delete output;
}

void Gate::logic(){
    if (Type == "NOT"){
        if (inputA->getState() == UNKNOWN){
        }
        if (inputA->getState() == HI){
            output->setState(LOW);
        }
        if (inputA->getState() == LOW){
            output->setState(HI);
        }
    }
    if (Type == "AND") {
        if (inputA->getState() == HI && inputB->getState() == HI){
            output->setState(HI);
        }
        else {
            output->setState(LOW);
        }
    }
    if (Type == "OR") {
        if (inputA->getState() == HI || inputB->getState() == HI){
            output->setState(HI);
        }
        else {
            output->setState(LOW);
        }
    }
    if (Type == "XOR"){
        if (inputA->getState() != inputB->getState()){
            output->setState(HI);
        }
        else
        {
            output->setState(LOW);
        }
    }
    if (Type == "NAND"){
        if (inputA->getState() == HI && inputB->getState() == HI){
            output->setState(LOW);
        }
        else{
            output->setState(HI);
        }
    }
    if (Type == "NOR"){
        if (inputA->getState() == LOW && inputB->getState() == LOW){
            output->setState(HI);
        }
        else{
            output->setState(LOW);
        }
    }
    if (Type == "XNOR"){
        if (inputA->getState() == inputB->getState()){
            output->setState(HI);
        }
        else
        {
            output->setState(LOW);
        }
    }
}

void Gate::setType(string t){
    Type = t;
}

void Gate::setDelay(int d){
    delay = d;
}

1 个答案:

答案 0 :(得分:0)

在c ++中,编译可执行文件时,编译器需要知道从哪里开始执行。为了实现这一点,您需要一个名为main的函数,并带有以下签名:

int main() { ... }

OR

int main(int argc, char** argv){ ... }

您没有此功能。将其添加到您的一个cpp文件中。