volatile 구조체 변수 사용시 에러

munhoney의 이미지

안녕하세요

Visual Studio (9.00 컴파일러를 이용하여 C++로 개발을 하고 있는데, volatile 구조체 문제가 있네요

내용인즉슨

a.cpp 에는

#include "stdafx.h"

extern volatile a_t gReg;

int main()
{
printf("reg : %d \n",gReg.a
}

b.cpp에는

#include "stdafx.h"

volatile a_t gReg;

void setReg()
{
a_t testReg = gReg;
}

라고 할때..

stdafx.h에는
단순하게

typedef struct {
short a;
short b;
short c;
} TestStruct_t;

typedef TestStruct_t a_t;

라고 하면

b.cpp의 a_t testReg = gReg; 부분에서 에러가 발생합니다.

에러 내용은
error C2440: 초기화 중 : 'volatile a_t'에서 'a_t'로 변환할수 없습니다.
입니다.

혹시 왜 이런 문제가 나는지 , 해결책은 무엇인지 알 수 있을까요...

익명_사용자의 이미지

에러메세지 그대로입니다.

해결책은
testReg의 타입을 a_t에서 volatile a_t로 바꾸세요.

volatile a_t testReg = gReg;

gilgil의 이미지

volatile int 는 복사가 가능하고
volatile user_defined_type은 복사가 안되는군요. -_-;

netionics의 이미지

a_t testReg = gReg;는 생성자를 호출하게 되어있습니다.

a_t 에는 아무런 생성자도 정의하지 않았기 때문에 TestStruct::TestStruct()와 TestStruct::TestStruct(const TestStruct&)가 기본으로 만들어 지겠죠.

하지만 어느 곳에도 volatile TestStruct객체를 넘길 수가 없습니다. 타입이 맞지 않으니까요.

이 문제를 해결하려면 TestStruct::TestStrect(volatile TestStruct&)를 만들어주거나

short a, short b, short c를 인자로 받는 생성자를 만들고 a_t testReg(gReg.a, gReg.b, gReg.c); 와 같이 호출해줘야 합니다.

이도 저도 못쓰면 a_t testReg = *const_cast(&gReg); 와 같이 캐스팅 해주는 방법을 쓸 수도 있겠네요.

:)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.