This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Copy constructor does not accept parameter by value


>Submitter-Id:
>Originator:    Arun Saini
>Organization:  HCL Technologies, Gurgaon, INDIA
>Confidential:  no
>Synopsis:      Copy constructor does not accept parameter by value
>Severity:      critical
>Priority:      high
>Category:      c++
>Class:         rejects-legal
>Release:       3.0 20010528 (prerelease)
>Environment:
System: Linux ntc 2.2.16-22 #1 Tue Aug 22 16:49:06 EDT 2000 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: /usr/local/src/gcc-20010528/configure
--enable-threads=posix --enable-shared
>Description:
I am reproducing a portion of code and the error reported by the compiler
when I try to compile it.

class Test
{
  public :
    int testVar;

  Test()
  {
    testVar = 0;
  }

  Test(Test& t)
  {
    testVar = t.testVar;
    t.testVar = 0;
  }

  Test(Test *pt)
  {
    testVar = pt->testVar;
    pt -> testVar = 0;
  }

  Test& operator=(Test& t)
  {
    testVar = t.testVar;
    t.testVar = 0;
  }

  Test& operator=(Test* pt)
  {
    testVar = pt->testVar;
    pt->testVar = 0;
  }
};

Test someFunc(void)
{
  Test tempTest;
  tempTest.testVar = 10;

  return tempTest;
}


int main()
{
  Test t1;
  t1 = someFunc();
  return 0;
}

When I compile this the error given is

g++ test.cpp
test.cpp: In function `int main()':
test.cpp:49: no match for `Test& = Test' operator
test.cpp:24: candidates are: Test& Test::operator=(Test&)
test.cpp:30:                 Test& Test::operator=(Test*)

>How-To-Repeat:
>Fix:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]