我正在尝试复制传递给复制构造函数的对象。我想访问传递给此函数的对象的成员变量的解引用值,但遇到错误“在'('令牌int * c = new int(other。(* pa))之前出现预期的unqualified-id;
该类已定义:
class Foo {
Public:
int *a, *b;
Foo(const Foo &); //copy constructor
}
我的功能已定义:
Foo::Foo(const Foo& other) {
int* c = new int(other.(*a));
int* d = new int(other.(*b));
}
主要定义:
Foo first(1,2);
Foo second(first);
答案 0 :(得分:5)
复制构造函数看起来像
Foo::Foo(const Foo& other) : a( new int( *other.a ) ), b( new int( *other.b ) )
{
}
这是一个演示程序
#include <iostream>
class Foo {
public:
int *a, *b;
Foo( int x, int y ) : a( new int( x ) ), b( new int( y ) )
{
}
Foo( const Foo &other ) : a( new int( *other.a ) ), b( new int( *other.b ) )
{
}
};
int main()
{
Foo first(1,2);
Foo second(first);
std::cout << *first.a << ", " << *first.b << '\n';
std::cout << *second.a << ", " << *second.b << '\n';
return 0;
}
其输出为
1, 2
1, 2
所有其他特殊成员函数,例如析构函数,我希望您可以定义自己。
答案 1 :(得分:0)
将值分配给对象成员。
library(tidyverse)
library(ggplot2)
library(shiny)
library(leaflet)
library(leafpop)
id <- c(1,1,1,1,2,2,3,3,3,4)
lat <- c(49.823, 49.823, 49.823, 49.823, 58.478, 58.478, 57.478 , 57.478 , 57.478, 38.551)
lng <- c(-10.854, -10.854, -10.854, -10.854, -11.655, -11.655, 2.021 , 2.021 , 2.021, 5.256)
type <- c("A","C","B","B","C","A","B","A","C","B")
date <- c(152.5,307.5,145,481,152,109.5,258.5,107.5,186.5,150)
start <- c(123,235,135,192,149,101,205,75,155,100)
stop <- c(182,380,155,289,155,218,312,140,218,200)
myData <- data.frame(id,type,date,start,stop,lat,lng)
chronogramme<- function(dataId){
dataFiltered<-filter(myData,id==dataId)
p<- ggplot(dataFiltered,aes(type,date))+
geom_linerange(aes(ymin=start,ymax=stop),size=5)+
coord_flip()
return(p)
}
ui <- fluidPage(
leafletOutput("map"),
plotOutput("plot")
)
server <- function(input, output, session) {
#Sortie map
output$map <- renderLeaflet({
leaflet()%>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(
layerId=~id,
data = myData,
lat = myData$lat,
lng = myData$lng,
radius = 5,
color = 'blue',
stroke = FALSE,
fillOpacity = 1,
group = 'markers'
)
})
observeEvent(input$map_marker_click,{
p <- chronogramme(input$map_marker_click$id)
isolate({
leafletProxy("map") %>% addPopupGraphs(list(p), group = 'markers')
})
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)