ISO C++ forbids delcaration of "[string]" with no type.

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3186 Views - Last Post: 27 September 2011 - 05:28 PM Rate Topic: -----

#1 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 12:57 PM

i am writing a class which will tell the user what notes are in a given key. but i instantly have 50+ errors. many of which are the same thing 4 errors repeating over and over... "ISO C++ forbids declaration of 'chromatic' with no type
ISO C++ forbids initialization of member 'chromatic'|
making 'chromatic' static|
invalid in-class initialization of static data member of non-integral type 'int [0]'|"
the following is my class

#ifndef _MUSICKEY_H
#define _MUSICKEY_H
#include<string>
using namespace std;
class musickey
{
    public:
        musickey();
        musickey(const musickey &);

        string SetKey();
        string SetTonality();
        string Scale();
    private:
        string chromatic[12];
        string formula;
        string temp;
        int next,last;

        chromatic[0] = "A";
        chromatic[1] = "A#";
        chromatic[2] = "B";
        chromatic[3] = "C";
        chromatic[4] = "C#";
        chromatic[5] = "D";
        chromatic[6] = "D#";
        chromatic[7] = "E";
        chromatic[8] = "F";
        chromatic[9] = "F#";
        chromatic[10]= "G";
        chromatic[11]= "G#";
};

musickey::musickey()
{
    formula = "WWHWWWH";
}

musickey::musickey(const &Object)
{

}

string musickey::SetKey(string inckey)
{
    for(int i = 0; chrmoatic[i] != inckey ; i++)
    {
        last = (i+1);
    }
    return(last);
}

string musickey::SetTonality(string inctone)
{
    if(inctone == "Major")
    {
        formula = "WWHWWWH";
    }
    if(inctone == "Minor")
    {
        formula = "WHWWHWW";
    }
}

string musickey::Scale()
{
       for(int i = 0; i < formula.length(); i++)
    {
        if(formula[i] == 'W')
        {
            next = 2;
        }
        if(formula[i] == 'H')
        {
            next = 1;
        }

        next += last;
        if(next > 11)
        {
            next -=12;
        }
        last = next;
        temp.append(chromatic[next]);
    }
return temp;
}
#endif



Is This A Good Question/Topic? 0
  • +

Replies To: ISO C++ forbids delcaration of "[string]" with no type.

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 01:00 PM

        chromatic[0] = "A";
        chromatic[1] = "A#";
        chromatic[2] = "B";
        chromatic[3] = "C";
        chromatic[4] = "C#";
        chromatic[5] = "D";
        chromatic[6] = "D#";
        chromatic[7] = "E";
        chromatic[8] = "F";
        chromatic[9] = "F#";
        chromatic[10]= "G";
        chromatic[11]= "G#";



You can only put declarations into the class body. The above statements aren't declarations, so the need to go into your constructor.
Was This Post Helpful? 1
  • +
  • -

#3 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 01:05 PM

thank you, now i am getting the following 4 errors..

I:\Computer Programming\C++\Key 2.0\musickey.h|57|error: prototype for 'std::string musickey::SetKey(std::string)' does not match any in class 'musickey'|
I:\Computer Programming\C++\Key 2.0\musickey.h|11|error: candidate is: std::string musickey::SetKey()|
I:\Computer Programming\C++\Key 2.0\musickey.h|66|error: prototype for 'std::string musickey::SetTonality(std::string)' does not match any in class 'musickey'|
I:\Computer Programming\C++\Key 2.0\musickey.h|12|error: candidate is: std::string musickey::SetTonality()|
I:\Computer Programming\C++\Key 2.0\musickey.h||In member function 'std::string musickey::Scale()':|
I:\Computer Programming\C++\Key 2.0\musickey.h|80|warning: comparison between signed and unsigned integer expressions|
||=== Build finished: 4 errors, 1 warnings ===|

This post has been edited by JackOfAllTrades: 11 September 2011 - 01:27 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 01:26 PM

The declaration in your class does not match the implementation [parameters].
Was This Post Helpful? 1
  • +
  • -

#5 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 01:32 PM

View PostKYA, on 10 September 2011 - 01:26 PM, said:

The declaration in your class does not match the implementation [parameters].

okay, i got all of this fixed. i went back and ran it, and it ran perfectly. then i went back, and changed some variable names. i made sure to change ALL the ones it would pertain too. i changed everything from "Tonality" to "Mode". now it compiles just fine, but it freezes when it gives the output.
Class
#ifndef _MUSICKEY_H
#define _MUSICKEY_H
#include<string>
using namespace std;
class musickey
{
    public:
        musickey();
        musickey(const musickey &);

        void SetKey(string);
        void SetMode(string);
        string Scale();
    private:
        string chromatic[12];
        string formula;
        string temp;
        int next,last;

};

musickey::musickey()
{
    formula = "WWHWWWH";
    chromatic[0] = "A";
    chromatic[1] = "A#";
    chromatic[2] = "B";
    chromatic[3] = "C";
    chromatic[4] = "C#";
    chromatic[5] = "D";
    chromatic[6] = "D#";
    chromatic[7] = "E";
    chromatic[8] = "F";
    chromatic[9] = "F#";
    chromatic[10]= "G";
    chromatic[11]= "G#";
}

musickey::musickey(const musickey &Object)
{
    chromatic[0] = "A";
    chromatic[1] = "A#";
    chromatic[2] = "B";
    chromatic[3] = "C";
    chromatic[4] = "C#";
    chromatic[5] = "D";
    chromatic[6] = "D#";
    chromatic[7] = "E";
    chromatic[8] = "F";
    chromatic[9] = "F#";
    chromatic[10]= "G";
    chromatic[11]= "G#";
}

void musickey::SetKey(string inckey)
{
    for(int i = 0; chromatic[i] != inckey ; i++)
    {
        last = i+1;
    }
}

void musickey::SetMode(string incmode)
{
    if(incmode == "Major")
    {
        formula = "WWHWWWH";
    }

    if(incmode == "Minor")
    {
        formula = "WHWWHWW";
    }

    if(incmode == "Dorian");
    {
        formula =  "whwwwhw";
    }

    if(incmode == "Phrygian");
    {
        formula = "hwwwhww";
    }
}

string musickey::Scale()
{
    cout<<temp[last];
    for(int i = 0; i < formula.length(); i++)
    {
        temp.append(chromatic[last]);
        temp.append(" ");
        if(formula[i] == ('W' || 'w'))
        {
            next = 2;
        }
        if(formula[i] == ('H' || 'h'))
        {
            next = 1;
        }
        next += last;
        if(next > 11)
        {
            next -=12;
        }
        last = next;
    }
return temp;
}
#endif



Main Program
#include <iostream>
#include <string>
#include "musickey.h"
using namespace std;

int main()
{
    musickey mykey;
    string key, mode;

    cout<<"Input Key: ";
    cin>>key;
    mykey.SetKey(key);

    cout<<"Input Mode: ";
    cin>>mode;
    mykey.SetMode(mode);

    cout<<mykey.Scale();
    cin.ignore(5000, '\n');
    cin.get();
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 01:42 PM

i successfully (thank the lord) wrote a class and main program. the ran perfectly (of coarse, not on the first try). then i went back and changed a variable name ( i did change all of them that pertained) and now, it runs but freezes on output. im not sure what the problem is.
Main.cpp
#include <iostream>
#include <string>
#include "musickey.h"
using namespace std;

int main()
{
    musickey mykey;
    string key, mode;

    cout<<"Input Key: ";
    cin>>key;
    mykey.SetKey(key);

    cout<<"Input Mode: ";
    cin>>mode;
    mykey.SetMode(mode);

    cout<<mykey.Scale();
    cin.ignore(5000, '\n');
    cin.get();
return 0;
}



Class
#ifndef _MUSICKEY_H
#define _MUSICKEY_H
#include<string>
using namespace std;
class musickey
{
    public:
        musickey();
        musickey(const musickey &);

        void SetKey(string);
        void SetMode(string);
        string Scale();
    private:
        string chromatic[12];
        string formula;
        string temp;
        int next,last;

};

musickey::musickey()
{
    formula = "WWHWWWH";
    chromatic[0] = "A";
    chromatic[1] = "A#";
    chromatic[2] = "B";
    chromatic[3] = "C";
    chromatic[4] = "C#";
    chromatic[5] = "D";
    chromatic[6] = "D#";
    chromatic[7] = "E";
    chromatic[8] = "F";
    chromatic[9] = "F#";
    chromatic[10]= "G";
    chromatic[11]= "G#";
}

musickey::musickey(const musickey &Object)
{
    chromatic[0] = "A";
    chromatic[1] = "A#";
    chromatic[2] = "B";
    chromatic[3] = "C";
    chromatic[4] = "C#";
    chromatic[5] = "D";
    chromatic[6] = "D#";
    chromatic[7] = "E";
    chromatic[8] = "F";
    chromatic[9] = "F#";
    chromatic[10]= "G";
    chromatic[11]= "G#";
}

void musickey::SetKey(string inckey)
{
    for(int i = 0; chromatic[i] != inckey ; i++)
    {
        last = i+1;
    }
}

void musickey::SetMode(string incmode)
{
    if(incmode == "Major")
    {
        formula = "WWHWWWH";
    }

    if(incmode == "Minor")
    {
        formula = "WHWWHWW";
    }

    if(incmode == "Dorian");
    {
        formula =  "whwwwhw";
    }

    if(incmode == "Phrygian");
    {
        formula = "hwwwhww";
    }
}

string musickey::Scale()
{
    cout<<temp[last];
    for(int i = 0; i < formula.length(); i++)
    {
        temp.append(chromatic[last]);
        temp.append(" ");
        if(formula[i] == ('W' || 'w'))
        {
            next = 2;
        }
        if(formula[i] == ('H' || 'h'))
        {
            next = 1;
        }
        next += last;
        if(next > 11)
        {
            next -=12;
        }
        last = next;
    }
return temp;
}
#endif


Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 01:46 PM

Threads merged. Please do not create multiple topics on the same question.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 02:45 PM

for(int i = 0; chromatic[i] != inckey ; i++)
// and if chromatic[i] == inckey is never true?
// your code just runs forever

// not a valid if then
if(incmode == "Dorian");
{
// if incmode doesn't match, you don't choke

// you don't even bother to check if
// formula or last ever got set
string musickey::Scale()
{
	// temp is empty at this point
	// this should crash
	cout<<temp[last];

	// it want to say this won't compile, but it actually will
	if(formula[i] == ('W' || 'w'))
	// since 'W' is non zero, the || statement will resolve to true
	// in C++, that will produce a value of 1
	// unless the char in formula[i]==1, you probably aren't getting far with this



There seems to be a fundamental misunderstanding of the point of a class. You'd do just as well with:
string getScale(const string &key, const string &mode);


This post has been edited by baavgai: 10 September 2011 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

#9 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 03:59 PM

honestly, ill be the only one using the program. so im not worried about checking to make sure the user input something acceptable. but how would you reccomend i proceed?

oh, and why isn't line 6 valid?

This post has been edited by JackOfAllTrades: 11 September 2011 - 01:27 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 10 September 2011 - 05:40 PM

You have a semicolon at the end of a couple of if statements.

075	    if(incmode == "Dorian");
076	    {
077	        formula =  "whwwwhw";
078	    }




You could default to Major :-

void musickey::SetMode(string incmode)
{
  // default to Major
  formula = "WWHWWWH";

  if(incmode == "Major")
  {
    formula = "WWHWWWH";
  }


Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 11 September 2011 - 05:52 AM

View PostTy Meador, on 10 September 2011 - 06:59 PM, said:

honestly, ill be the only one using the program. so im not worried about checking to make sure the user input something acceptable.


Wait, you enter data perfectly every single time? You're my hero. ;)

Just because this was amusing, I'd do it like so:
#include <iostream>

using namespace std;

string getScale(const string &key, const string &mode) {
	const int ChromaticSize = 12;
	const string chromatic[ChromaticSize] = {
		"A","A#","B","C","C#","D","D#","E","F","F#","G","G#"
	};
	int last;
	string result;
	
	for(last=0; last<ChromaticSize; last++) {
		if (chromatic[last]==key) { break; }
	}
	if (last==ChromaticSize) {
		cout << key << " is not a valid key." << endl;
	} else {
		string formula;
		if(mode=="Major") { formula = "WWHWWWH";
		} else if(mode=="Minor") { formula = "WHWWHWW";
		} else if(mode=="Dorian") { formula =  "whwwwhw";
		} else if(mode=="Phrygian") { formula = "hwwwhww";
		}
		if (formula.empty()) {
			cout << mode << " is not a valid mode." << endl;
		} else {
			last++;
			for(int i = 0; i < formula.length(); i++) {
				char ch = formula[i];
				result.append(chromatic[last]);
				result.append(" ");
				int next;
				if(ch=='W'||ch=='w') {
					next = 2;
				} else if(ch=='H'||ch=='h') {
					next = 1;
				}
				next += last;
				if(next > 11) {
					next -=12;
				}
				last = next;
			}
		}
	}
	return result;
}

void test(const string &key, const string &mode) {
	cout << "Key = " << key << endl;
	cout << "Mode = " << mode << endl;
	string scale = getScale(key, mode);
	if (!scale.empty()) { 
		cout << "Scale = " << scale << endl;
	}
	cout << endl;
}

int main(void) {
	cout << endl;
	test("F#", "Major");
	test("J#", "Major");
	test("F#", "Foo");
	return 0;
}



Actually, I'm not 100% sure of your logic here. I might prefer this:
bool getScale(const string &key, const string &mode, string &result) {
	const int ChromaticSize = 12;
	const string chromatic[ChromaticSize] = {
		"A","A#","B","C","C#","D","D#","E","F","F#","G","G#"
	};
	int keyIndex;
	result = "";
	
	for(keyIndex=0; keyIndex<ChromaticSize; keyIndex++) {
		if (chromatic[keyIndex]==key) { break; }
	}
	if (keyIndex==ChromaticSize) {
		cout << key << " is not a valid key." << endl;
		return false;
	}
	
	
	string formula;
	if(mode=="Major") { formula = "WWHWWWH";
	} else if(mode=="Minor") { formula = "WHWWHWW";
	} else if(mode=="Dorian") { formula =  "whwwwhw";
	} else if(mode=="Phrygian") { formula = "hwwwhww";
	} else {
		cout << mode << " is not a valid mode." << endl;
		return false;
	}
	
	for(int i = 0; i < formula.length(); i++) {
		if (i!=0) { result.append(" "); }
		result.append(chromatic[keyIndex % ChromaticSize]);
		char ch = formula[i];
		keyIndex += (ch=='W'||ch=='w') ? 2 : 1;
	}
	return true;
}


void test(const string &key, const string &mode) {
	cout << "Key = " << key << endl;
	cout << "Mode = " << mode << endl;
	string scale;
	if (getScale(key, mode, scale)) { 
		cout << "Scale = " << scale << endl;
	}
	cout << endl;
}



Note, I intentionally varied how the function flowed, to give you an idea of a few approaches. You could have a dictionary of modes. For user entry, it would be easier to pick from a list than have to know it automatically. Also less likely to enter junk.

Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#12 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 11 September 2011 - 01:03 PM

this helps....kinda. i undersatnd what your doing, to an extent. would you mind explaining it line by line if you have time?

This post has been edited by JackOfAllTrades: 11 September 2011 - 01:26 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#13 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 11 September 2011 - 01:21 PM

This is my code so far BTW
#ifndef _MUSICKEY_H
#define _MUSICKEY_H
#include<string>
using namespace std;
class musickey
{
    public:
        musickey();
        musickey(const musickey &);

        bool SetKey(string);
        bool SetMode(string);
        string Scale();
    private:
        string chromatic[12];
        string formula;
        string temp;
        unsigned int next,last;

};

musickey::musickey()
{
    formula = "wwhwwwh";
    const int chromaticsize = 12;
    const string chromatic[chromaticsize] =
    {
        "A","A#","B","C","C#","D","D#","E","F","F#","G","G#"
    };
}

musickey::musickey(const musickey &Object)
{
    const int chromaticsize = 12;
    const string chromatic[chromaticsize] =
    {
        "A","A#","B","C","C#","D","D#","E","F","F#","G","G#"
    };
}

bool musickey::SetKey(string inckey)
{
    int i;
    for(i = 0; chromatic[i] != inckey ; i++)
    {
        last = i+1;
    }
    if( i == 12)
    {
        cout<<inckey<<" is not a valid key.";
        return false;
    }
    return true;
    temp = chromatic[last];
}

bool musickey::SetMode(string incmode)
{
    formula = "wwhwwwh";
    if(incmode == "Major")
    {
        formula = "wwhwwwh";
    }

    if(incmode == "Minor")
    {
        formula = "whhhwhh";
    }

    if(incmode == "Dorian")
    {
        formula = "whwwwhw";
    }

    if(incmode == "Phrygian")
    {
        formula = "hwwwhww";
    }
    else
    {
        cout<<incmode<<" is not a valid mode.";
        return false;
    }
return true;
}

string musickey::Scale()
{
    temp = chromatic[last];
    for(int i = 0; i < formula.length(); i++)
    {
        temp.append(chromatic[last]);
        temp.append(" ");
        if(formula[i] == ('W' || 'w'))
        {
            next = 2;
        }
        if(formula[i] == ('H' || 'h'))
        {
            next = 1;
        }
        next += last;
        if(next > 11)
        {
            next -= 12;
        }
        last = next;
    }
return temp;
}
#endif


Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6109
  • View blog
  • Posts: 23,666
  • Joined: 23-August 08

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 11 September 2011 - 01:28 PM

Ty, PLEASE stop quoting the post immediately before yours, especially for a one sentence reply!
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: ISO C++ forbids delcaration of "[string]" with no type.

Posted 11 September 2011 - 02:29 PM

Quote

this helps....kinda. i undersatnd what your doing, to an extent. would you mind explaining it line by line if you have time?


I would suggest, you tell us what each line of the code is doing and we will correct you if you are wrong. Or ask specific questions about what really confuses you.

Also in your latest code in your constructor:
musickey::musickey()
{
    formula = "wwhwwwh";
    const int chromaticsize = 12;
    const string chromatic[chromaticsize] =
    {
        "A","A#","B","C","C#","D","D#","E","F","F#","G","G#"
    };
}


The variables chromaticsize and chromatic are local to this function and therefore any changes you make to these variables will be lost when the function ends. They are not the same variables as the variables defined in your class definition. If you want to set the values of your class member variables do not include the const int. You seem to do this is other functions as well.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2