Defining a object by this way - Test object();
up vote
-3
down vote
favorite
There are some errors,and I can not find them,so I search your help,thanks.
Errors:obj1.print();obj2.print();
Tips:expressions must contain class types.
Changing code and get following output:
TestClass1:0
TestClass2
Attention:
- Do not alter the main function.
- Do not add or delete rows.
- Do not modify the structure of program.
`
struct TestClass1 {
TestClass1(int i=0) {
m_i = i;
}
void print() {
cout << "TestClass1:" << m_i << endl;
}
int m_i;
};
class TestClass2 {
public:
TestClass2(int) {}
void print() {
cout << "TestClass2" << endl;
}
private:
~TestClass2() { }
};
int main() {
TestClass1 obj1();
TestClass2 obj2();
obj1.print();
obj2.print();
return 0;
}
`
c++
add a comment |
up vote
-3
down vote
favorite
There are some errors,and I can not find them,so I search your help,thanks.
Errors:obj1.print();obj2.print();
Tips:expressions must contain class types.
Changing code and get following output:
TestClass1:0
TestClass2
Attention:
- Do not alter the main function.
- Do not add or delete rows.
- Do not modify the structure of program.
`
struct TestClass1 {
TestClass1(int i=0) {
m_i = i;
}
void print() {
cout << "TestClass1:" << m_i << endl;
}
int m_i;
};
class TestClass2 {
public:
TestClass2(int) {}
void print() {
cout << "TestClass2" << endl;
}
private:
~TestClass2() { }
};
int main() {
TestClass1 obj1();
TestClass2 obj2();
obj1.print();
obj2.print();
return 0;
}
`
c++
1
There are some errors... what are the errors?
– Blaze
Nov 20 at 14:45
Vexing parse forTestClass1 obj1();
, -> function declaration
– Jarod42
Nov 20 at 14:48
This will never work without changing the contents of the main function (see: most vexing parse).
– Rene
Nov 20 at 14:48
@Rene: You forget power of (:evil:) MACRO.
– Jarod42
Nov 20 at 15:37
@Blaze Errors: obj1.print();obj2.print(); Tips:expressions must contain class types
– Kuusi
Nov 21 at 14:20
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
There are some errors,and I can not find them,so I search your help,thanks.
Errors:obj1.print();obj2.print();
Tips:expressions must contain class types.
Changing code and get following output:
TestClass1:0
TestClass2
Attention:
- Do not alter the main function.
- Do not add or delete rows.
- Do not modify the structure of program.
`
struct TestClass1 {
TestClass1(int i=0) {
m_i = i;
}
void print() {
cout << "TestClass1:" << m_i << endl;
}
int m_i;
};
class TestClass2 {
public:
TestClass2(int) {}
void print() {
cout << "TestClass2" << endl;
}
private:
~TestClass2() { }
};
int main() {
TestClass1 obj1();
TestClass2 obj2();
obj1.print();
obj2.print();
return 0;
}
`
c++
There are some errors,and I can not find them,so I search your help,thanks.
Errors:obj1.print();obj2.print();
Tips:expressions must contain class types.
Changing code and get following output:
TestClass1:0
TestClass2
Attention:
- Do not alter the main function.
- Do not add or delete rows.
- Do not modify the structure of program.
`
struct TestClass1 {
TestClass1(int i=0) {
m_i = i;
}
void print() {
cout << "TestClass1:" << m_i << endl;
}
int m_i;
};
class TestClass2 {
public:
TestClass2(int) {}
void print() {
cout << "TestClass2" << endl;
}
private:
~TestClass2() { }
};
int main() {
TestClass1 obj1();
TestClass2 obj2();
obj1.print();
obj2.print();
return 0;
}
`
c++
c++
edited Nov 21 at 14:26
asked Nov 20 at 14:41
Kuusi
11
11
1
There are some errors... what are the errors?
– Blaze
Nov 20 at 14:45
Vexing parse forTestClass1 obj1();
, -> function declaration
– Jarod42
Nov 20 at 14:48
This will never work without changing the contents of the main function (see: most vexing parse).
– Rene
Nov 20 at 14:48
@Rene: You forget power of (:evil:) MACRO.
– Jarod42
Nov 20 at 15:37
@Blaze Errors: obj1.print();obj2.print(); Tips:expressions must contain class types
– Kuusi
Nov 21 at 14:20
add a comment |
1
There are some errors... what are the errors?
– Blaze
Nov 20 at 14:45
Vexing parse forTestClass1 obj1();
, -> function declaration
– Jarod42
Nov 20 at 14:48
This will never work without changing the contents of the main function (see: most vexing parse).
– Rene
Nov 20 at 14:48
@Rene: You forget power of (:evil:) MACRO.
– Jarod42
Nov 20 at 15:37
@Blaze Errors: obj1.print();obj2.print(); Tips:expressions must contain class types
– Kuusi
Nov 21 at 14:20
1
1
There are some errors... what are the errors?
– Blaze
Nov 20 at 14:45
There are some errors... what are the errors?
– Blaze
Nov 20 at 14:45
Vexing parse for
TestClass1 obj1();
, -> function declaration– Jarod42
Nov 20 at 14:48
Vexing parse for
TestClass1 obj1();
, -> function declaration– Jarod42
Nov 20 at 14:48
This will never work without changing the contents of the main function (see: most vexing parse).
– Rene
Nov 20 at 14:48
This will never work without changing the contents of the main function (see: most vexing parse).
– Rene
Nov 20 at 14:48
@Rene: You forget power of (:evil:) MACRO.
– Jarod42
Nov 20 at 15:37
@Rene: You forget power of (:evil:) MACRO.
– Jarod42
Nov 20 at 15:37
@Blaze Errors: obj1.print();obj2.print(); Tips:expressions must contain class types
– Kuusi
Nov 21 at 14:20
@Blaze Errors: obj1.print();obj2.print(); Tips:expressions must contain class types
– Kuusi
Nov 21 at 14:20
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Without changing the structure of the program I can only point out the problems.
TestClass2
does not have a default constructor to initialize inTestClass2 obj2();
TestClass2
destructor is private: "error: temporary of type 'TestClass2' has private destructor"- You don't indicate what standard you are compiling to, C++17 the preferred method of initializing objects is using braces eg:
TestClass1 obj1{};
or nothing:TestClass1 obj1;
: "warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]" - Constructors taking a single argument should be declared
explicit
to prevent unwanted conversion, unless that is what you want.
With those changes I get the following output when run:
TestClass1:0
TestClass2
Thank you for your assistance,I changed constructor of TestClass2 to a default constructor and have made private destructor to public,but it is forbided to change TestClass1 obj1() to any ohter types,so I also can not run the code successful.
– Kuusi
Nov 21 at 14:38
If you are forbidden to change the initialization idiom, then what standard are you required to compile with?
– Richard
Nov 23 at 13:04
Any standard can
– Kuusi
Nov 28 at 14:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395454%2fdefining-a-object-by-this-way-test-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Without changing the structure of the program I can only point out the problems.
TestClass2
does not have a default constructor to initialize inTestClass2 obj2();
TestClass2
destructor is private: "error: temporary of type 'TestClass2' has private destructor"- You don't indicate what standard you are compiling to, C++17 the preferred method of initializing objects is using braces eg:
TestClass1 obj1{};
or nothing:TestClass1 obj1;
: "warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]" - Constructors taking a single argument should be declared
explicit
to prevent unwanted conversion, unless that is what you want.
With those changes I get the following output when run:
TestClass1:0
TestClass2
Thank you for your assistance,I changed constructor of TestClass2 to a default constructor and have made private destructor to public,but it is forbided to change TestClass1 obj1() to any ohter types,so I also can not run the code successful.
– Kuusi
Nov 21 at 14:38
If you are forbidden to change the initialization idiom, then what standard are you required to compile with?
– Richard
Nov 23 at 13:04
Any standard can
– Kuusi
Nov 28 at 14:34
add a comment |
up vote
0
down vote
Without changing the structure of the program I can only point out the problems.
TestClass2
does not have a default constructor to initialize inTestClass2 obj2();
TestClass2
destructor is private: "error: temporary of type 'TestClass2' has private destructor"- You don't indicate what standard you are compiling to, C++17 the preferred method of initializing objects is using braces eg:
TestClass1 obj1{};
or nothing:TestClass1 obj1;
: "warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]" - Constructors taking a single argument should be declared
explicit
to prevent unwanted conversion, unless that is what you want.
With those changes I get the following output when run:
TestClass1:0
TestClass2
Thank you for your assistance,I changed constructor of TestClass2 to a default constructor and have made private destructor to public,but it is forbided to change TestClass1 obj1() to any ohter types,so I also can not run the code successful.
– Kuusi
Nov 21 at 14:38
If you are forbidden to change the initialization idiom, then what standard are you required to compile with?
– Richard
Nov 23 at 13:04
Any standard can
– Kuusi
Nov 28 at 14:34
add a comment |
up vote
0
down vote
up vote
0
down vote
Without changing the structure of the program I can only point out the problems.
TestClass2
does not have a default constructor to initialize inTestClass2 obj2();
TestClass2
destructor is private: "error: temporary of type 'TestClass2' has private destructor"- You don't indicate what standard you are compiling to, C++17 the preferred method of initializing objects is using braces eg:
TestClass1 obj1{};
or nothing:TestClass1 obj1;
: "warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]" - Constructors taking a single argument should be declared
explicit
to prevent unwanted conversion, unless that is what you want.
With those changes I get the following output when run:
TestClass1:0
TestClass2
Without changing the structure of the program I can only point out the problems.
TestClass2
does not have a default constructor to initialize inTestClass2 obj2();
TestClass2
destructor is private: "error: temporary of type 'TestClass2' has private destructor"- You don't indicate what standard you are compiling to, C++17 the preferred method of initializing objects is using braces eg:
TestClass1 obj1{};
or nothing:TestClass1 obj1;
: "warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]" - Constructors taking a single argument should be declared
explicit
to prevent unwanted conversion, unless that is what you want.
With those changes I get the following output when run:
TestClass1:0
TestClass2
answered Nov 20 at 15:11
Richard
8,81221423
8,81221423
Thank you for your assistance,I changed constructor of TestClass2 to a default constructor and have made private destructor to public,but it is forbided to change TestClass1 obj1() to any ohter types,so I also can not run the code successful.
– Kuusi
Nov 21 at 14:38
If you are forbidden to change the initialization idiom, then what standard are you required to compile with?
– Richard
Nov 23 at 13:04
Any standard can
– Kuusi
Nov 28 at 14:34
add a comment |
Thank you for your assistance,I changed constructor of TestClass2 to a default constructor and have made private destructor to public,but it is forbided to change TestClass1 obj1() to any ohter types,so I also can not run the code successful.
– Kuusi
Nov 21 at 14:38
If you are forbidden to change the initialization idiom, then what standard are you required to compile with?
– Richard
Nov 23 at 13:04
Any standard can
– Kuusi
Nov 28 at 14:34
Thank you for your assistance,I changed constructor of TestClass2 to a default constructor and have made private destructor to public,but it is forbided to change TestClass1 obj1() to any ohter types,so I also can not run the code successful.
– Kuusi
Nov 21 at 14:38
Thank you for your assistance,I changed constructor of TestClass2 to a default constructor and have made private destructor to public,but it is forbided to change TestClass1 obj1() to any ohter types,so I also can not run the code successful.
– Kuusi
Nov 21 at 14:38
If you are forbidden to change the initialization idiom, then what standard are you required to compile with?
– Richard
Nov 23 at 13:04
If you are forbidden to change the initialization idiom, then what standard are you required to compile with?
– Richard
Nov 23 at 13:04
Any standard can
– Kuusi
Nov 28 at 14:34
Any standard can
– Kuusi
Nov 28 at 14:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395454%2fdefining-a-object-by-this-way-test-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
There are some errors... what are the errors?
– Blaze
Nov 20 at 14:45
Vexing parse for
TestClass1 obj1();
, -> function declaration– Jarod42
Nov 20 at 14:48
This will never work without changing the contents of the main function (see: most vexing parse).
– Rene
Nov 20 at 14:48
@Rene: You forget power of (:evil:) MACRO.
– Jarod42
Nov 20 at 15:37
@Blaze Errors: obj1.print();obj2.print(); Tips:expressions must contain class types
– Kuusi
Nov 21 at 14:20