语法错误:OO C 中的向量数组++

发布于 2024-12-08 18:56:24 字数 1561 浏览 0 评论 0原文

我已经有了我想要创建的 HashTable 类的轮廓。我从 Visual Studio 中收到 3 个错误输出,但我在这里看不到问题所在。我对 C++ 中的面向对象相当陌生,所以这可能是我错过的东西。它声称我的向量数组有问题。错误是:

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;
}

I've got an outline of a HashTable class I'm trying to make. I'm getting 3 errors output from Visual Studio, but I can't see the problem here. I'm fairly new to OO in C++ so it's probably something i've missed. It claims there is a problem with my array of vectors. The errors are:

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

Here's my complete class, it's pretty empty right now:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光礼记 2024-12-15 18:56:24

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

这意味着代码中包含的 将被忽略,因此编译 vector 会导致错误。

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

The C++ here is valid (once you fill in the empty functions). The problem is with how Visual C++ uses precompiled headers. When you use precompiled headers (the default setting), the Visual C++ compiler expects the first line of each implementation file to be #include "stdafx.h", and doesn't compile anything that appears before that.

This means the the include of <vector> in your code is ignored, and thus compiling vector<int> causes an error.

If you move the line #include "stdafx.h" to the top this should compile. Or you can disable precompiled headers in the project settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文