字符串数组表
table of strings array
#include <iostream>
#include "HtmlTable.h"
using namespace std;
int main()
{
cout << "Content-type: text/html" << endl << endl;
HtmlTable t(2,3);
t.insert(2,1, "one");
t.insert(1,2, "two");
t.insert(2,3, "three");
t.print();
return 0;
}
#ifndef HTMLTABLE_H
#define HTMLTABLE_H
#include <string>
#include <iostream>
using namespace std;
class HtmlTable
{
public:
HtmlTable(int y, int x)
{
}
void print()
{
cout << "<table>";
for (row=0; row<y; row++)
{
cout << "<tr>";
for (col=0; col<x; col++)
{
cout << "<table border='1'>";
cout << m_Table[y][x];
}
cout << "</td>";
}
cout << "</table>";
}
void insert(int row, int col, string text)
{
y = row;
x = col;
z = text;
m_Table[y][x] = {{z,z,z,z},{z,z,z,z},{z,z,z,z},{z,z,z,z}};
}
protected:
private:
string m_Table[100][100];
int row;
int col;
string text;
int x;
int y;
string z;
int get_x = x;
int get_y = x;
};
#endif // HTMLTABLE_H
我必须创建一个字符串的 2d 数组。有一个插入函数可以将字符串插入数组中的某个位置。然后打印函数应该打印一个表格,其中包含相应框中的单词。输出应该是这样的:
|____| 二 |__
____|
|一|____| 三|
我被给了 int 主要,无法更改任何东西。
我目前的问题是空插入。 错误是:
与"运算符="不匹配 '((HtmlTable*(this(->HtmlTable::m_Table[((HtmlTable*(this(->HtmlTable::y]
我过去的尝试只打印了最后一个春天,并对表格中的每个盒子重复。我错误地执行了什么数组?我的打印功能也不正确吗?
这一行:
m_Table[y][x] = {{z,z,z,z},{z,z,z,z},{z,z,z,z},{z,z,z,z}};
是完全错误的。 m_Table
是 2D 数组或字符串,因此m_Table[y][x]
只是一个std::string
。你应该写:m_Table[y][x] = z
.
但是您的代码中还有许多其他问题:
- 在构造函数中传递数组维度
HtmlTable
但忽略它们 - 当
z, row, col, text
不存储状态时,将它们声明为成员变量:它们应该是成员函数的局部变量 这两行
int get_x = x; int get_y = x;
声明未使用的成员变量并尝试初始化它们,这是不正确的。成员变量应该在构造函数中初始化(整型静态常量变量除外(
-
print
从 0 到y
进行循环row
,从 0 到x
col
循环(如果初始化x
和y
是正确的(,但始终写入m_Table[y][x]
而不是m_Table[row][col]
而且你的print
方法错了...因为你从未初始化过 x 和 y。构造函数应为:
HtmlTable(int y, int x): x(x), y(y)
{
}
并且您不应该在插入中修改它们:
void insert(int row, int col, string text)
{
m_Table[row][col] = text;
}
数组在 C++ 中索引为 0。您的主要内容应包含:
t.insert(1,0, "one");
t.insert(0,1, "two");
t.insert(1,2, "three");
顺便说一句,生成的 HTML 是不正确的:您不关闭<tr>
标签,不打开<td>
标签,并以奇怪的方式嵌套<table>
标签,但这将是另一回事......
相关文章:
- 使用 jsoncpp 解析 json 数组字符串
- C++ 对象数组字符串输入在控制台上不起作用
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 无法区分 JSON 对象/数组/字符串
- 为什么数组(字符串类型)的大小是 24 字节,带有单个空格元素
- 如何使用数组字符串进行输入验证
- (C++) 打印字符数组字符串的单词时出现问题
- 使用Back_inserter从任何位置的输入字符中使用Back_inserter进行过滤数组字符串
- C 函数反转字符数组字符串
- 如何在 c++ 中创建数组(字符串 [] )的向量
- 如何返回数组字符串的指针
- char数组字符串混淆
- 如何从字符数组字符串中提取空格
- 是否可以将 Bitset<8> 的值复制到数组字符串而不转换它们?
- C++数组字符串函数
- 如何在函数中操作字符数组(字符串)的指针,该指针在C / C ++中作为参数传递
- visual将音频文件存储到数组/字符串流C++中
- c++在char数组(字符串)上将what替换为
- 将数组字符串转换为intger c++(初学者)
- 需要设置数组字符串长度,使其恰好为13