I have some code here that doesn't seem to be linking properly. I have searched, and several places have suggested it is a problem with int main(). Not really sure what my problem is. I am pretty new to programming, and I have tried a few different things. Any help would be great!
I have four files: Wire.h, Wire.cpp, Gate.h, and Gate.cpp.
This is the 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
This is 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;
}
This is 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
This is 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;
}
In c++, when compiling an executable, the compiler needs to know where to start execution. In order to make this happen, you need a function called main
with this signature:
int main() { ... }
OR
int main(int argc, char** argv){ ... }
You don't have this function. Add it to one of your cpp files.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.