這段代碼中有一個奇怪的語法錯誤。我不明白它或任何錯誤。
#pragma once
#include <iostream>
class Tree {
public:
Node* root;
void InputTree() {
int n, father;
int nd;
int child;
char dir;
std::cin >> n;
int** tree = new int* [n];
for (int i = 0; i < n; i ) {
std::cin >> nd;
tree[i] = new int[3];
tree[i][0] = nd;
tree[i][1] = -1;
tree[i][2] = -1;
}
for (int i = 0; i < n - 1; i ) {
std::cin >> father >> child >> dir;
if (dir == 'L')
tree[father][1] = child;
else
tree[father][2] = child;
}
Initialize(tree, 0);
}
private:
void Initialize(int** tree, int headIndex) {
if (headIndex != -1) {
root->value = tree[headIndex][0];
root->right_tree->Initialize(tree, tree[headIndex][2]);
root->left_tree->Initialize(tree, tree[headIndex][1]);
}
}
};
class Node {
public:
int value;
Tree* left_tree;
Tree* right_tree;
};
這是 Visual Studio 的構建輸出:
Build started...
1>------ Build started: Project: CppMainProject, Configuration: Debug Win32 ------
1>CppMainProject.cpp
1>tstream.h(5,6): error C2143: syntax error: missing ';' before '*'
1>tstream.h(5,6): error C4430: missing type specifier - int assumed. Note: C does not support default-int
1>tstream.h(5,12): error C2238: unexpected token(s) preceding ';'
1>tstream.h(33,4): error C2065: 'root': undeclared identifier
1>tstream.h(34,4): error C2065: 'root': undeclared identifier
1>tstream.h(35,4): error C2065: 'root': undeclared identifier
1>Done building project "CppMainProject.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
uj5u.com熱心網友回復:
在諸如root->value = tree[headIndex][0];之Node類的行中,您在告訴編譯器該類是什么之前嘗試使用該類。您可以在類的完整定義之前將變數宣告為指向類的指標(但您需要類的前向宣告才能這樣做),但在提供完整的類定義之后才能取消參考此類指標。
在你的代碼,你可以定義移動Node類之前的Tree類; 但是,由于使用Tree指標,您必須提供一個Tree作為類的前向宣告。
#pragma once
#include <iostream>
class Tree; // Forward declaration that this is a class...
class Node {
public:
int value;
Tree* left_tree; // ... so we can now declare pointers to "Tree"
Tree* right_tree;
};
class Tree {
public:
Node* root; // We can declare a pointer
void InputTree()
{
// ... and then the rest of your code as is ...
一些值得一讀:我什么時候可以使用前向宣告?
uj5u.com熱心網友回復:
Node宣告時仍未定義root。嘗試在 Tree 類中移動 Node Class,我建議您將其更改為 struct 如下:
#pragma once
#include <iostream>
class Tree {
public:
void InputTree() {
int n, father;
int nd;
int child;
char dir;
std::cin >> n;
int** tree = new int* [n];
for (int i = 0; i < n; i ) {
std::cin >> nd;
tree[i] = new int[3];
tree[i][0] = nd;
tree[i][1] = -1;
tree[i][2] = -1;
}
for (int i = 0; i < n - 1; i ) {
std::cin >> father >> child >> dir;
if (dir == 'L')
tree[father][1] = child;
else
tree[father][2] = child;
}
Initialize(tree, 0);
}
private:
struct Node {
int value;
Tree* left_tree;
Tree* right_tree;
};
Node* root;
void Initialize(int** tree, int headIndex) {
if (headIndex != -1) {
root->value = tree[headIndex][0];
root->right_tree->Initialize(tree, tree[headIndex][2]);
root->left_tree->Initialize(tree, tree[headIndex][1]);
}
}
};
uj5u.com熱心網友回復:
如果你希望它都在一個檔案中,向前宣告Node并移動 . 定義Tree::Initialize(...)之后的定義Node。或者只是向前宣告Node并使用典型的 .h 檔案 / .cpp 檔案拆分。
單檔案版本是這樣的:
class Node; // <= this is a forward declaration...
class Tree {
public:
Node* root;
void InputTree() {
// ....
}
private:
void Initialize(int** tree, int headIndex);
};
class Node {
public:
int value;
Tree* left_tree;
Tree* right_tree;
};
void Tree::Initialize(int** tree, int headIndex)
{
// ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/375361.html
上一篇:C讀取訪問沖突-指標
