我无法将头文件嵌入到Main.cpp中

时间:2018-11-23 12:35:52

标签: c++

我有一个用C ++完成的作业。我使用Main.cpp和头文件(datastruct.h)对其进行了编码。作业已完成,编译并成功运行;但是提交规则使我只能使用一个main.cpp。当我尝试将代码包含在main.cpp的标头中时,我得到:

[main] C:\cygnus\cygwin-b20\H-i586-cygwin32\bin\g++.exe 1000 (0) handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
[main] g++ 1000 (0) handle_exceptions: Dumping stack trace to g++.exe.core

请注意:我的问题不仅是关于此错误,还在于我仅将标头代码嵌入到main.cpp中时才遇到此错误。当它们分开时,它可以正常工作。 这是我的main.cpp,其中不包含标头代码:

#include <iostream>
#include <fstream>
#include "datastruct.h"
using namespace std;

int main(int argc, char *argv[]) 
{
  Game myGame;
  myGame.initializer(argv[1]);
  cout << myGame.gamePlay();
  myGame.cleaner();

  return 0;
}

这是“ datastruct.h”:

#ifndef DATASTRUCT_H
#define DATASTRUCT_H

#include <iostream>
#include <fstream>
using namespace std;

int abs(int k) {
  if(k < 0) k = -k;
  return k;
}

struct Card {
  int value;
  Card* prev;
};

struct Deck {
  Card* top ;
  int cardNum;
  void addCard(int xd);
  int dropCard();
  void create();
  void clear();
  void print();
};

void Deck::clear(/* arguments */) {
  Card *p;
  while(top)
  {
    p = top;
    top = top -> prev;
    delete p;
  }
}

int Deck::dropCard(/* arguments */) {
  Card* cardPtr;
  int returnVal = top -> value;
  cardPtr = top;
  top = top -> prev;
  delete cardPtr;
  cardNum--;
  return returnVal;
}

void Deck::create() {
  cardNum = 0;
  top = NULL;
}

void Deck::addCard(int xd) {
  Card* newCard;
  newCard = new struct Card;
  newCard -> value = xd;
  newCard -> prev = top;
  top = newCard;
  cardNum++;
}

struct Game {
  Deck* p1;
  Deck* p2;
  Deck* table;
  Deck* bin;
  void initializer(char* filename);
  void cleaner();
  void gamePrint();
  void p1gives();
  void p2gives();
  int gamePlay();
};

int Game::gamePlay()
{
  int cardTaken;

  while (true)
  {
    if((p1->cardNum ==0) || (p2->cardNum ==0) || (table->cardNum ==0)) break;

    cardTaken = table->dropCard();
    if (cardTaken < 0) {
      for (int i = 0; i < abs(cardTaken); i++) {
        if(p1->top == NULL) break;
        p1gives();
      }
    } else {
      for (int i = 0; i < cardTaken; i++) {
        if(p2->top == NULL) break;
        p2gives();
      }
    }

    if((p1->cardNum ==0) || (p2->cardNum ==0) || (table->cardNum ==0)) break;

    cardTaken = table->dropCard();
    if (cardTaken < 0) {
      for (int i = 0; i < abs(cardTaken); i++) {
        if((p1->top == NULL) || (p2->top == NULL)) break;
        p2gives();
      }
    } else {
      for (int i = 0; i < cardTaken; i++) {
        if((p1->top == NULL) || (p2->top == NULL)) break;
        p1gives();
      }
    }
  }
  return (bin -> cardNum);
}

void Game::p1gives()
{
  if(p2 -> top == NULL)
    p2 -> addCard(p1 -> dropCard());
  else if (p1 -> top -> value > p2 -> top -> value)
    p2 -> addCard(p1 -> dropCard());
  else if (p1 -> top -> value <= p2 -> top -> value)
    bin -> addCard(p1 -> dropCard());
}

void Game::p2gives()
{
  if (p1 -> top == NULL)
    p1 -> addCard(p2 -> dropCard());
  else if(p2 -> top -> value > p1 -> top -> value)
    p1 -> addCard(p2 -> dropCard());
  else if(p2 -> top -> value <= p1 -> top -> value)
    bin -> addCard(p2 -> dropCard());
}

void Game::cleaner()
{
  p1 -> clear();
  p2 -> clear();
  table -> clear();
  bin -> clear();

  delete p1;
  delete p2;
  delete table;
  delete bin;
}

void Game::initializer(char* filename)
{
  ifstream myFile(filename);
  int tableDeckCount, playerDeckCount;
  myFile >> tableDeckCount;
  myFile >> playerDeckCount;

  p1 = new struct Deck;
  p1 -> create();
  p2 = new struct Deck;
  p2 -> create();
  table = new struct Deck;
  table -> create();
  bin = new struct Deck;
  bin -> create();

  for (int i = 0; i < tableDeckCount; i++) {
    int x;
    myFile >> x;
    table -> addCard(x);
  }
  for (int i = 0; i < playerDeckCount; i++) {
    int x;
    myFile >> x;
    p1 -> addCard(x);
  }
  for (int i = 0; i < playerDeckCount; i++) {
    int x;
    myFile >> x;
    p2 -> addCard(x);
  }
}

void Deck::print(/* arguments */) {
  Card* traverse;
  traverse = top;
  while (traverse) {
    cout << traverse -> value << " , " ;
    traverse = traverse -> prev;
  }
  cout << endl;
}

void Game::gamePrint()
{
  cout << "P1:" << endl;
  p1 -> print();
  cout << "P2:" << endl;
  p2 -> print();
  cout << "TABLE:" << endl;
  table -> print();
  cout << "BIN:" << endl;
  bin -> print();
}
#endif

我需要在main.cpp中包含标头,但是在复制代码时出现错误。有人可以帮我吗?

预期的工作示例:

>g++ -std=c++0x -Wall -Wextra -Werror main.cpp -o cardgame 
>./cardgame example.game 
1

example.game文件:

1 3
-2
6
7
8
1
5
4

1 个答案:

答案 0 :(得分:0)

在Windows上以提升的特权运行编译? (右键单击以管理员身份运行)/ edit。右键单击sh.exe以获取cygwin,然后转到兼容性并选中“以管理员身份运行”框)

也供那些否定投票的人参考:https://developer.qualcomm.com/forum/qdn-forums/mobile-technologies/multimedia-optimization-hexagon-sdk/toolsinstallation/27100