大佬教程收集整理的这篇文章主要介绍了如何从 2 个不同的文件访问结构数组?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我将如何去做这样的事情?
header.h
struct myStruct;
extern myStruct arraY[];
@H_167_2@main.cpp
#include "header.h"
struct myStruct{
int a = 0;
};
myStruct array[5];
array[0].a = 1;
file2.cpp
#include "header.h"
std::cout << array[0].a;
在您尝试访问其内容的任何地方都需要知道 @H_79_7@myStruct 的大小和布局,因此您必须在 .h 文件中定义它,或者仅使用不透明指针和专用函数来访问它。
此外,在 C++ 中使用 C 数组有些过时。您应该改用 STL 容器,例如 std::array<T,N>
或 std::vector<T>
。
嗯,首先你应该把 struct
声明放在头文件中。
其次,您不能将赋值放在数组或 std::cout
语句中,只是在文件中松散。我假设你这样做是为了短代码,而不是因为你不知道它需要在函数中,但我指出它以防万一。
三、struct声明需要分号
你要做的是有一个头文件:
header.hpp:
#pragma once
struct MyStruct {
int a = 0;
};
extern MyStruct array[5];
这里的 extern
表示数据在其他地方。所以让我们把它放在其他地方。
header.cpp:
#include "header.hpp"
MyStruct array[5];
@H_167_2@main.cpp:
#include "header.hpp"
void someFunction() {
...
array[0].a = 1;
...
}
file2.cpp:
#include <iostream>
#include "header.hpp"
void someOtherFunction() {
...
std::cout << array[0].a;
...
}
,
我将如何去做这样的事情?
除了修复您尝试中的错误之外,您可以通过做一些类似的事情来做一些类似的事情。以下是错误列表:
rm(list = ls())
# Fetch Options for SELEctize Input
# source('scripts/pingDB.R')
# sql__namelist_nba <<- paste(readLines("sql/nba/namelist_nba.txt"),collapse=" ")
# namelist_nba <<- getData(sql__namelist_nba)
# namelist_nba <<- namelist_nba[order(namelist_nba$full_Name),]
# namelist_nba <<- setNames(namelist_nba$player_id,namelist_nba$full_Name)
# hardcode first 20 results for stackoverflow post
namelist_nba <- c(`A.C. Green` = 920L,`A.J. Bramlett` = 1920L,`A.J. Davis` = 203667L,`A.J. Guyton` = 2062L,`Aaron Best` = 1628700L,`Aaron Brooks` = 201166L,`Aaron Craft` = 203905L,`Aaron Epps` = 1629200L,`Aaron Gordon` = 203932L,`Aaron Gray` = 201189L,`Aaron Harrison` = 1626151L,`Aaron Holiday` = 1628988L,`Aaron Jackson` = 1628935L,`Aaron Johnson` = 203638L,`Aaron McKie` = 243L,`Aaron Miles` = 101223L,`Aaron nesR_465_11845@ith` = 1630174L,`Aaron Pettway` = 202925L,`Aaron Thomas` = 1628045L,`Aaron White` = 1626206L)
# create body and sidebar
ui_body <- dashboardBody()
# note use of NS() and modules
ns2 <- NS('nba_player_profile')
ui_sidebar <- dashboardSidebar(
sidebarMenu(
id = "sidebarMenu",menuItem("these Pages",tabName = "team",menuSubItem("Player Profile",tabName = "player_profile_nba"),conditionalPanel(
"input.sidebarMenu === 'player_profile_nba'",class = NULL,SELEctizeInput(inputId = ns2("player_input"),label = 'Player Search: ',choices = NULL)
)
)
)
)
# server module for "player profile" page
server__player_profile <- function(id) {
moduleServer(
id,function(input,output,session) {
updateSELEctizeInput(
session,inputId = session$ns('player_input'),choices = namelist_nba,SELEcted = namelist_nba[1],server = TRUE)
}
)
}
# shinyserver is where we combine all of our server modules...
server <- shinyServer(function(input,session) {
observeEvent(input$sidebarMenu,{
print(paste0("sidebarMenu tabName: ",input$sidebarMenu))
})
server__player_profile(id = 'nba_player_profile')
})
# ui is where we combine header,sidebar,body
ui <- dashboardPage(
title="Dashboard title",dashboardHeader(
title = div("Our Databoard header title"),titleWidth = 300),ui_sidebar,ui_body
)
# and return / run the app
app <- shinyApp(ui = ui,server = server)
runApp(app)
的定义。.c
。或者您打算使用cout
?在这种情况下,您必须包含 std::cout
才能使用它,并使用适当的命名空间。<iostream>
中,您使用类 file2.c
的方式取决于 @H_79_7@myStruct 的定义,而不提供该定义。array[0].a = 1;
是表达式语句。表达式语句可能不在命名空间范围内,而必须在块范围内。在您的程序中,这些表达式语句位于命名空间范围内,这使得程序格式错误。以上是大佬教程为你收集整理的如何从 2 个不同的文件访问结构数组?全部内容,希望文章能够帮你解决如何从 2 个不同的文件访问结构数组?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。