GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Aug-2005, 13:22
hard2clone hard2clone is offline
New Member
 
Join Date: Aug 2005
Posts: 7
hard2clone is on a distinguished road

"cannot convert from 'class X *' to 'class X'" ??


What's the problem here? when i try to create a new instance of my class Signature it's giving me the following error:


c:\PredicateLogicTrainer\signature.h(104) : error C2440: 'initializing' : cannot convert from 'class Signature *' to 'class Signature'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.

main.exe - 1 error(s), 0 warning(s)

it refers to the following line:
CPP / C++ / C Code:
Signature signature = new Signature(name, unaryArray, binaryArray, constantArray) ;


I'm lost... is this the right way to create a class??

Here's the full code:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
using namespace std;
#ifndef SIGNATURE_H
#define SIGNATURE_H

class Signature
{
public:
	Signature(		
		string nName, 
		string unaryRels[3],
		string binaryRels[3],
		string constants[3]);
	Signature();


Signature makeSignature(string newName, int unaryRs, int binaryRs, int constants) {


	string name = "Untitled";
	cout << "\nThank you for creating a signature. So, what would you like to call this signature?";
	cin >> name;
	if (name == ""){
		name = "untitled";
	}


	string numberTail = "th";

	string unaryArray[3]; 

	for (int u = 0; u < unaryRs; u++) {
		cout << "\nPlease enter the name of the " << u + 1;
		if (u == 0) {
			numberTail = "st";
		}
		if (u == 1) {
			numberTail = "nd";
		}
		if (u == 2) {
			numberTail = "rd";
		}
		cout << numberTail << " unary relation symbol. If you don't want one, just press Enter.\n";
		cin >> unaryArray[u];
	}
	
	
//	numberTail = "th";

	string binaryArray[3];
		
	for (int b = 0; b < binaryRs; b++) {
		cout << "\nPlease enter the name of the " << b + 1;
		if (b == 0) {
			numberTail = "st";
		}
		if (b == 1) {
			numberTail = "nd";
		}
		if (b == 2) {
			numberTail = "rd";
		}
		cout << numberTail << " binary relation symbol. If you don't want one, just press Enter.\n";
		cin >> binaryArray[b];
	}

	string constantArray[3];
		
	for (int c = 0; c < constants; c++) 
	{
		cout << "\nPlease enter the name of the " << c + 1;
		if (c == 0) {
			numberTail = "st";
		}
		if (c == 1) {
			numberTail = "nd";
		}
		if (c == 2) {
			numberTail = "rd";
		}
		cout << numberTail << " constant. If you don't want one, just press Enter.\n";
		cin >> constantArray[c];
	}


	for (int a = 0; a< 3; a++) {
		cout << "\n\nThe arrays look like this: ";
		cout << "\n" << unaryArray[a];
		cout << "\n" << binaryArray[a];
		cout << "\n" << constantArray[a];
	}
	
	



//	for (int c = 0, c < constants, c++) {

//		cout << "\nPlease enter the "



Signature signature = new Signature(name, unaryArray, binaryArray, constantArray) ;

return signature;
};

};

#endif
  #2  
Old 03-Aug-2005, 13:59
CMan's Avatar
CMan CMan is offline
New Member
 
Join Date: Jul 2005
Posts: 19
CMan is on a distinguished road
in C++, the "new" keyword is used to create pointers as far as I know, so maybe you should do:
Code:
Signature signature(name, unaryArray, binaryArray, constantArray) ;
instead of
Code:
Signature signature = new Signature(name, unaryArray, binaryArray, constantArray) ;
It's different from Java and C# where you have to use new to declare an object of a class.
  #3  
Old 03-Aug-2005, 14:04
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by hard2clone
CPP / C++ / C Code:
Signature makeSignature(string newName, int unaryRs, int binaryRs, int constants) {
// ...
Signature signature = new Signature(name, unaryArray, binaryArray, constantArray) ;

return signature;
}
How about this?

CPP / C++ / C Code:
Signature makeSignature(string newName, int unaryRs, int binaryRs, int constants) {
// ...
return Signature(name, unaryArray, binaryArray, constantArray) ;
}
  #4  
Old 03-Aug-2005, 14:07
hard2clone hard2clone is offline
New Member
 
Join Date: Aug 2005
Posts: 7
hard2clone is on a distinguished road
Quote:
Originally Posted by CMan
in C++, the "new" keyword is used to create pointers as far as I know, so maybe you should do:
Code:
Signature signature(name, unaryArray, binaryArray, constantArray) ;
instead of
Code:
Signature signature = new Signature(name, unaryArray, binaryArray, constantArray) ;
It's different from Java and C# where you have to use new to declare an object of a class.


Thanks buddy, You're a star!!

I've got other errors now, but that one was killing me.

Cheers!
  #5  
Old 03-Aug-2005, 14:30
hard2clone hard2clone is offline
New Member
 
Join Date: Aug 2005
Posts: 7
hard2clone is on a distinguished road
Now, the above works!!

However, in the main file, I want to create a signature... so naturally i type:

CPP / C++ / C Code:
Signature s; // to declare a signature called s
s.makeSignature (3, 3, 3); // btw, i changed the parameters to the makeSignature above to only the last three, as i gain the fourth parameter from a cin call

but i get the following error:

Linking...
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Signature::Signature(void)" (??0Signature@@QAE@XZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Signature::Signature(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class st
d::allocator<char> > * const,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const)" (??0Signature@@QAE@V?$basi
c_string@DU?$char_traits@D@std@@V?$allocator@D@2@@ std@@QAV12@11@Z)
Debug/main.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.


Note, this is not a compile error, it's a linking error.... i don't have a clue what that is...
  #6  
Old 03-Aug-2005, 15:44
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
I believe it's the linker's way of saying, "Hey, buddy! You told me to expect these:"
CPP / C++ / C Code:
  Signature(    
    string nName, 
    string unaryRels[3],
    string binaryRels[3],
    string constants[3]);
  Signature();
"Where are they? What gives?"

In other words, you need to define these functions. Mere declarations are not enough. Otherwise every one of us would have written this, run it, and quit coding to retire on some beach:

CPP / C++ / C Code:
int SendMeMillions(int count);
  #7  
Old 03-Aug-2005, 16:06
CMan's Avatar
CMan CMan is offline
New Member
 
Join Date: Jul 2005
Posts: 19
CMan is on a distinguished road
Quote:
Originally Posted by hard2clone
Thanks buddy
No problem.

For the linker problem though, I agree with Dave. Do something like this for each undefined function:

CPP / C++ / C Code:
    Signature() 
    {
         //Your implementation here, or leave it empty if no initialization is required.
    }; //see, we implemented the ctor

Quote:
Originally Posted by Dave Sinkula
Otherwise every one of us would have written this, run it, and quit coding to retire on some beach:

CPP / C++ / C Code:
int SendMeMillions(int count);

CPP / C++ / C Code:
int SendMeMillions(int count)
{  ofstream fout("MyPocket");
   fout<<count;
}

there we go
  #8  
Old 03-Aug-2005, 16:32
05gtp 05gtp is offline
Awaiting Email Confirmation
 
Join Date: Aug 2005
Location: Clinton Township (Detroit Area), MI
Posts: 13
05gtp will become famous soon enough

got this to work


this compiles, links and executes...
I agree, you have no member function bodies, unless you have them in a seperate header file?

CPP / C++ / C Code:

#ifndef _SIGNATURE_H_INCLUDED
#define _SIGNATURE_H_INCLUDED

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#pragma once

class Signature
{
public:
	Signature(void) {}
	Signature(string nName, string unaryRels[3], string binaryRels[3], string constants[3]) {}
	virtual ~Signature(void) {}

	Signature Signature::makeSignature(string newName, int unaryRs, int binaryRs, int constants)
	{

		string name = "Untitled";
		cout << "\nThank you for creating a signature.\nSo, what would you like to call this signature?  ";
		cin >> name;

		string postfix;
		if (name == "") name = "untitled";

		string unaryArray[3];
		for (int u = 0; u < unaryRs; u++) {
			cout << "\nPlease enter the name of the " << u + 1;
			if (u == 0) postfix = "st";
			if (u == 1) postfix = "nd";
			if (u == 2) postfix = "rd";
			cout << postfix << " unary relation symbol.\nIf you don't want one, just press Enter.\n";
			cin >> unaryArray[u];
		}

		string binaryArray[3];
		for (int b = 0; b < binaryRs; b++) {
			cout << "\nPlease enter the name of the " << b + 1;
			if (b == 0) postfix = "st";
			if (b == 1) postfix = "nd";
			if (b == 2) postfix = "rd";
			cout << postfix << " binary relation symbol.\nIf you don't want one, just press Enter.\n";
			cin >> binaryArray[b];
		}

		string constantArray[3];
		for (int c = 0; c < constants; c++) {
			cout << "\nPlease enter the name of the " << c + 1;
			if (c == 0) postfix = "st";
			if (c == 1) postfix = "nd";
			if (c == 2) postfix = "rd";
			cout << postfix << " constant.\nIf you don't want one, just press Enter.\n";
			cin >> constantArray[c];
		}

		for (int a = 0; a < 3; a++) {
			cout << "\n\nThe arrays look like this: ";
			cout << "\n" << unaryArray[a];
			cout << "\n" << binaryArray[a];
			cout << "\n" << constantArray[a];
			cout << "\n";
		}

		Signature signature(name, unaryArray, binaryArray, constantArray);

		return signature;
	}
};

#endif // _SIGNATURE_H_INCLUDED

OR, quick edit, I prefer this one... Up2U.

CPP / C++ / C Code:

#ifndef _SIGNATURE_H_INCLUDED
#define _SIGNATURE_H_INCLUDED

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#pragma once

class Signature
{
public:
	Signature(void) {}
	Signature(string nName, string unaryRels[3], string binaryRels[3], string constants[3]) {}
	virtual ~Signature(void) {}

	void Signature::Menu(const unsigned int cnt, const string arrType)
	{
		string postfix;
		cout << "\nPlease enter the name of the " << cnt + 1;
		if (cnt == 0) postfix = "st";
		if (cnt == 1) postfix = "nd";
		if (cnt == 2) postfix = "rd";
		cout << postfix << " " << arrType << " relation symbol.\nIf you don't want one, just press Enter.\n";
	}

	Signature Signature::makeSignature(string newName, int unaryRs, int binaryRs, int constants)
	{

		string name = "Untitled";
		cout << "\nThank you for creating a signature.\nSo, what would you like to call this signature?  ";
		cin >> name;

		if (name == "") name = "untitled";

		string unaryArray[3];
		for (int u = 0; u < unaryRs; u++) {
			Menu(u, "unary");
			cin >> unaryArray[u];
		}

		string binaryArray[3];
		for (int b = 0; b < binaryRs; b++) {
			Menu(b, "binary");
			cin >> binaryArray[b];
		}

		string constantArray[3];
		for (int c = 0; c < constants; c++) {
			Menu(c, "constant");
			cin >> constantArray[c];
		}

		for (int a = 0; a < 3; a++) {
			cout << "\n\nThe arrays look like this: ";
			cout << "\n" << unaryArray[a];
			cout << "\n" << binaryArray[a];
			cout << "\n" << constantArray[a];
			cout << "\n";
		}

		Signature signature(name, unaryArray, binaryArray, constantArray);

		return signature;
	}
};

#endif // _SIGNATURE_H_INCLUDED
  #9  
Old 03-Aug-2005, 16:49
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Hopefully what was posted is not actually in a header. If so, I'd recommend to the OP to quit now and learn how it's supposed to be done -- no code in headers! (With exceptions for templates at least.)

And no using namespace std; in headers either!
  #10  
Old 03-Aug-2005, 16:58
05gtp 05gtp is offline
Awaiting Email Confirmation
 
Join Date: Aug 2005
Location: Clinton Township (Detroit Area), MI
Posts: 13
05gtp will become famous soon enough

Dave, I agree...


All classes should have a header file of the class definition and a cpp source file that contains the code. This is how I solved it, but I put it back as it was originally, roughly, posted. Also, avoid the namespace issue of name clashing, such as std::count.
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Introduction to .NET LuciWiz .NET Forum 5 09-Aug-2007 04:53
Opinion on my code and a c++ class question FlipNode C++ Forum 7 07-Feb-2006 08:15
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 C++ Forum 2 19-Dec-2004 06:06
hashing help saiz66 C++ Forum 1 06-Jul-2004 06:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.