【问题标题】:Syntax error: Array of Vectors in OO C++语法错误:OO C++ 中的向量数组
【发布时间】:2011-12-01 09:01:32
【问题描述】:

我有一个我正在尝试制作的HashTable 课程的大纲。我从 Visual Studio 得到 3 个错误输出,但在这里看不到问题。我对 C++ 中的 OO 相当陌生,所以这可能是我错过的东西。它声称我的向量数组存在问题。错误是:

error C2143: syntax error : missing ';' before '<'  line 10
error C2238: unexpected token(s) preceding ';'  line 10
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   line 10

这是我的完整课程,现在很空:

#include <iostream>
#include <vector>
#include "stdafx.h"
using namespace std;

class HashTable
{
private:
  const static int buckets = 100;
  vector<int> hashTable[buckets];    //Internal storage

  int hash(int toHash);   //Performs hash function

public:
  HashTable();   //Constructor
  HashTable(int s);   //Constructor
  ~HashTable();   //Destructor

  void add(int toAdd);    //Adds an element to the HashTable
  void remove(int toDelete);    //Deletes an element from the HashTable
  bool search(int toSearch);    //Returns true if element in HashTable, false otherwise
  int getSize();   //Returns size of HashTable
  void print();    //Prints current state of the hashtable

  //TODO more methods...?




};

//Definitions...

HashTable::HashTable() 
{
}

HashTable::~HashTable() 
{
    //cout << "Destroyed" << endl;
}

void HashTable::add(int toAdd)
{

  //elements[hash(toAdd)] = toAdd;

}

void HashTable::remove(int toDelete)
{

}


bool HashTable::search(int toSearch)
{

}

int HashTable::getSize()
{
  //return size;
}


void HashTable::print()
{

}


int main()
{  

  return 0;
}

【问题讨论】:

  • 你为什么不用vector &lt;vector &lt;int&gt; &gt; hashTable;
  • 只在类体内声明桶,在类声明外定义
  • 如果删除 #include "stdafx.h" 会发生什么?在 VC++ 2010 中,我得到的唯一错误与未返回声明值的函数有关。
  • @e-MEE:我相信它是一个向量数组

标签: c++ arrays oop vector


【解决方案1】:

这里的 C++ 是有效的(一旦你填写了空函数)。问题在于 Visual C++ 如何使用预编译头文件。当您使用预编译的标头(默认设置)时,Visual C++ 编译器期望每个实现文件的第一行为 #include "stdafx.h",并且不会编译任何出现在此之前的内容。

这意味着您的代码中包含的&lt;vector&gt; 被忽略,因此编译vector&lt;int&gt; 会导致错误。

如果您将行 #include "stdafx.h" 移到顶部,这应该可以编译。或者您可以在项目设置中禁用预编译头文件。

【讨论】:

  • 啊,很好发现,这解决了问题。忘了它必须在顶部。这一定也是我无法在前面的方法定义中使用cout 的原因。谢谢!
猜你喜欢
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 2016-08-17
  • 2010-12-06
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
相关资源
最近更新 更多