我在 Visual Studio 中收到错误代码:
struct coordinates {
int x;
int y;
};
struct coordinates point;
point.x = 5;
point.y = 3;
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我初始化point.xand point.yinmain()和/或如果我给出这样的点值,它就会起作用:struct coordinates point = {5, 3}。为什么不能point.x在point.y之外初始化main()?
我已经使用 C 大约一年了,最后决定学习 C++。我正在尝试像这样实现我自己的链表类:
#include <cstdio>
#include <cstdlib>
struct linked_list_node
{
int value;
struct linked_list_node* next;
};
class LinkedList
{
linked_list_node* head;
public:
LinkedList add(int value)
{
printf("add called\n");
if (head == nullptr)
{
head = new linked_list_node;
head->value = value;
head->next = nullptr;
}
else
{
linked_list_node* curr = head;
while (curr->next != nullptr) curr = curr->next;
curr->next = new linked_list_node;
curr->next->value = value;
curr->next->next = nullptr;
}
return *this;
}
int get(size_t index)
{
linked_list_node* curr = head; …Run Code Online (Sandbox Code Playgroud)